Skip to content

libc/stdio: stream conformance fixes - #22179

Merged
GUIDINGLI merged 8 commits into
apache:masterfrom
Shanks0224:libc-stdio
Nov 25, 2026
Merged

GUIDINGLI merged 8 commits into
apache:masterfrom
Shanks0224:libc-stdio

Conversation

@Shanks0224

@Shanks0224 Shanks0224 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Note: Please adhere to Contributing Guidelines.

Summary

Fixes to stdio stream handling so that it follows C and POSIX:

  • ungetc() and fseek() clear the EOF indicator; reads stop while the EOF
    indicator is set.
  • freopen() with a NULL path flushes the stream and updates the stream mode.
  • fdopen() enables line buffering only for terminals; new streams start with
    an empty pushback buffer.
  • Correct flockfile()/funlockfile() usage in puts() and
    fputws_unlocked().
  • STDIO_DISABLE_BUFFERING defaults to DEFAULT_SMALL.

The scanf fixes moved to #22240 and the fflush() change to #22241.

Impact

  • The 53 in-tree configurations that select DEFAULT_SMALL and FILE_STREAM
    without setting STDIO_DISABLE_BUFFERING (for example olimexino-stm32:nsh,
    stm32f103-minimum:* and rv-virt:smp) now build without stdio buffering, and
    a STDIO_BUFFER_SIZE they set (nucleo-f302r8:can) no longer applies.
  • The defconfigs that the new default makes non-canonical are refreshed:
    phy6222:nsh, phy6222:phy_sbp and stm32ldiscovery:chrono drop
    CONFIG_STDIO_DISABLE_BUFFERING=y, now the default, and nucleo-f302r8:can
    and breadxavr:ws2812 drop CONFIG_STDIO_BUFFER_SIZE, now hidden. Their
    resulting .config does not change.
  • With STDIO_LINEBUFFER, streams of files that are not terminals, opened with
    fopen() or fdopen(), are now fully buffered instead of line buffered.
  • No impact on documentation or hardware.

Testing

Build host: Linux x86_64.

  • sim:nsh builds with no warning.
  • fdopen() of a FIFO as "w", fputs("hello"), freopen(NULL, "r"),
    fclose(): the 5 bytes are written, as on glibc.
  • tools/refresh.sh on all 142 defconfigs with DEFAULT_SMALL: no changes
    left. tools/testbuild.sh with stm32ldiscovery (nsh, chrono) passes; before
    the refresh, chrono failed the defconfig check as in CI.
  • stm32ldiscovery:chrono, nucleo-f302r8:can, phy6222:nsh and phy6222:phy_sbp
    build with arm-none-eabi-gcc.

@github-actions github-actions Bot added Area: OS Components OS Components issues Size: M The size of the change in this PR is medium labels Nov 23, 2026
@github-actions

github-actions Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

hifive1-revb

  • flash: .text -1,036 B (-1.2%, 85,868 B / 4,194,304 B, total: 2% used)
  • sram: .bss -256 B (-6.4%, 3,764 B / 16,384 B, total: 23% used)

mps2-an500-citest

  • flash: .text +24 B (+0.0%, 248,000 B / 4,194,304 B, total: 6% used)

qemu-armv8a

  • Code: .text.fdopen +28 B (+0.0%, 378,208 B)

qemu-intel64

  • Code: .text +21 B (+0.0%, 8,707,357 B)

s698pm-dkit

  • Code: .text +64 B (+0.0%, 410,960 B)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Comment thread libs/libc/stdio/lib_libfflush.c Resolved
Comment thread libs/libc/stdio/lib_libfflush.c Resolved
Comment thread libs/libc/stdio/lib_freopen.c Resolved
Comment thread libs/libc/stdio/Kconfig Resolved

@jerpelea jerpelea left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please split the PR in logical separate PRs
stream conformance fixes
scanf
fflush behavior

@Shanks0224 Shanks0224 changed the title libc/stdio: stream conformance fixes, scanf and fflush behavior libc/stdio: stream conformance fixes Nov 24, 2026
@Shanks0224

Copy link
Copy Markdown
Contributor Author

please split the PR in logical separate PRs stream conformance fixes scanf fflush behavior

@jerpelea Split as requested: scanf is #22240 and fflush is #22241; this PR keeps the stream conformance fixes.

@github-actions github-actions Bot added Size: S The size of the change in this PR is small and removed Size: M The size of the change in this PR is medium labels Nov 24, 2026
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

@casaroli please review again.

jerpelea
jerpelea previously approved these changes Nov 24, 2026
@Shanks0224

Copy link
Copy Markdown
Contributor Author

@casaroli PTAL

@Shanks0224

Copy link
Copy Markdown
Contributor Author

@xiaoxiang781216 Pushed a fix: the new STDIO_DISABLE_BUFFERING default made five small defconfigs non-canonical (phy6222:nsh, phy6222:phy_sbp, stm32ldiscovery:chrono, nucleo-f302r8:can, breadxavr:ws2812), which failed the defconfig check in CI. They are refreshed in the commit that changes the default, and the branch is rebased onto master.

Jackapyun and others added 8 commits November 25, 2026 16:39
When getc() reads a file stream up to EOF, the EOF indicator of the
stream is set.  A successful ungetc() pushes a character back, so
POSIX requires the end-of-file indicator to be cleared.

Clear the indicator once the character has been pushed back.

Signed-off-by: anpeiyun <[email protected]>
Signed-off-by: ganjing <[email protected]>
According to POSIX, when pathname is a null pointer, freopen() shall
attempt to change the mode of the stream to that specified by mode, as
if using the name of the file currently associated with the stream.
The file descriptor got the new mode, but the stream kept the old one.

POSIX also requires freopen() to first flush the stream as if by
fflush(stream).  This branch did not, so once the stream had the new
mode, data still buffered for writing was dropped when a write stream
was reopened as "r".  Flush it before the mode changes.

https://pubs.opengroup.org/onlinepubs/9699919799/functions/freopen.html

Signed-off-by: anpeiyun <[email protected]>
Signed-off-by: ganjing <[email protected]>
POSIX says that fgetc(), fgets(), gets() and fread() return without
reading when the end-of-file indicator of the stream is set.  NuttX
ignored the indicator and read from the file again.  Check the indicator
before reading and return at once if it is set.

https://pubs.opengroup.org/onlinepubs/9699919799/functions/fgetc.html
https://pubs.opengroup.org/onlinepubs/9699919799/functions/fread.html

Signed-off-by: anpeiyun <[email protected]>
Signed-off-by: ganjing <[email protected]>
POSIX says that a successful fseek() clears the end-of-file indicator of
the stream.  NuttX left it set, and since reads now stop while the
indicator is set, a read after seeking back into the file would fail.

https://pubs.opengroup.org/onlinepubs/9699919799/functions/fseek.html

Signed-off-by: anpeiyun <[email protected]>
Signed-off-by: ganjing <[email protected]>
fdopen() enabled line buffering unconditionally.  Enable it only when
the file descriptor refers to a terminal, as required for the default
buffering mode of a newly opened stream.

fopen() is built on fdopen(), so with CONFIG_STDIO_LINEBUFFER the streams
of files that are not terminals, opened with fopen() or fdopen(), are now
fully buffered instead of line buffered, as C requires for a stream that
does not refer to an interactive device.

Signed-off-by: pengyinjie <[email protected]>
Signed-off-by: ganjing <[email protected]>
Guarantee that all new streams start with empty pushback buffers
by explicitly clearing any ungetc characters during stream
construction.

Signed-off-by: anpeiyun <[email protected]>
Signed-off-by: ganjing <[email protected]>
…cked().

puts() locks stream, its copy of stdout, but unlocked stdout, which is
evaluated again.  Unlock stream, so that the unlock always matches the
lock.

fputws_unlocked() released the stream lock on a write error although it
never takes it.  fputws() holds the lock around the call and releases it
afterwards, so a write error released the lock twice, and an application
calling fputws_unlocked() directly released a lock it did not hold.
Remove that funlockfile().

Signed-off-by: ligd <[email protected]>
Signed-off-by: ganjing <[email protected]>
Tiny (DEFAULT_SMALL) systems are size-sensitive and rarely use
buffered stdio on hot output paths. Default the option to
DEFAULT_SMALL so small systems drop the associated static stdio
code automatically; non-small systems keep buffering on.

This changes the in-tree configurations that select DEFAULT_SMALL and
FILE_STREAM without setting STDIO_DISABLE_BUFFERING (53 of them, for
example olimexino-stm32:nsh, stm32f103-minimum:* and rv-virt:smp): they
now build without stdio buffering, and a STDIO_BUFFER_SIZE they set, as
nucleo-f302r8:can does, no longer applies.

Refresh the defconfigs that the new default makes non-canonical: those
that set STDIO_DISABLE_BUFFERING, which is now the default for them, or
STDIO_BUFFER_SIZE, which is now hidden.

Signed-off-by: zhangyuan29 <[email protected]>
Signed-off-by: ganjing <[email protected]>
@GUIDINGLI
GUIDINGLI merged commit 0943639 into apache:master Nov 25, 2026
58 checks passed
@Shanks0224
Shanks0224 deleted the libc-stdio branch November 26, 2026 02:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: OS Components OS Components issues Board: arm Board: avr Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants