Repository navigation
libc/stdio: stream conformance fixes - #22179
Merged
Merged
Conversation
Shanks0224
requested review from
Donny9,
anchao,
jerpelea,
michallenc,
pkarashchenko,
pussuw and
xiaoxiang781216
as code owners
November 23, 2026 07:28
|
jerpelea
approved these changes
Nov 23, 2026
xiaoxiang781216
approved these changes
Nov 23, 2026
casaroli
requested changes
Nov 23, 2026
jerpelea
requested changes
Nov 23, 2026
jerpelea
left a comment
Contributor
There was a problem hiding this comment.
please split the PR in logical separate PRs
stream conformance fixes
scanf
fflush behavior
xiaoxiang781216
previously approved these changes
Nov 23, 2026
Shanks0224
force-pushed
the
libc-stdio
branch
from
November 24, 2026 05:36
9049423 to
979fefa
Compare
This was referenced Nov 24, 2026
Shanks0224
force-pushed
the
libc-stdio
branch
from
November 24, 2026 05:54
979fefa to
8ce1d98
Compare
Contributor
Author
xiaoxiang781216
previously approved these changes
Nov 24, 2026
Contributor
|
@casaroli please review again. |
jerpelea
previously approved these changes
Nov 24, 2026
Contributor
Author
|
@casaroli PTAL |
Shanks0224
dismissed stale reviews from jerpelea and xiaoxiang781216
via
November 25, 2026 05:11
113b702
Shanks0224
force-pushed
the
libc-stdio
branch
from
November 25, 2026 05:12
8ce1d98 to
113b702
Compare
Shanks0224
requested review from
GUIDINGLI,
acassis,
gustavonihei and
raiden00pl
as code owners
November 25, 2026 05:12
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. |
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]>
Shanks0224
force-pushed
the
libc-stdio
branch
from
November 25, 2026 08:40
113b702 to
57c73c5
Compare
jerpelea
approved these changes
Nov 25, 2026
casaroli
approved these changes
Nov 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note: Please adhere to Contributing Guidelines.
Summary
Fixes to stdio stream handling so that it follows C and POSIX:
ungetc()andfseek()clear the EOF indicator; reads stop while the EOFindicator 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 withan empty pushback buffer.
flockfile()/funlockfile()usage inputs()andfputws_unlocked().STDIO_DISABLE_BUFFERINGdefaults toDEFAULT_SMALL.The scanf fixes moved to #22240 and the
fflush()change to #22241.Impact
DEFAULT_SMALLandFILE_STREAMwithout setting
STDIO_DISABLE_BUFFERING(for example olimexino-stm32:nsh,stm32f103-minimum:* and rv-virt:smp) now build without stdio buffering, and
a
STDIO_BUFFER_SIZEthey set (nucleo-f302r8:can) no longer applies.phy6222:nsh, phy6222:phy_sbp and stm32ldiscovery:chrono drop
CONFIG_STDIO_DISABLE_BUFFERING=y, now the default, and nucleo-f302r8:canand breadxavr:ws2812 drop
CONFIG_STDIO_BUFFER_SIZE, now hidden. Theirresulting
.configdoes not change.STDIO_LINEBUFFER, streams of files that are not terminals, opened withfopen()orfdopen(), are now fully buffered instead of line buffered.Testing
Build host: Linux x86_64.
sim:nshbuilds 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.shon all 142 defconfigs withDEFAULT_SMALL: no changesleft.
tools/testbuild.shwith stm32ldiscovery (nsh, chrono) passes; beforethe refresh, chrono failed the defconfig check as in CI.
build with arm-none-eabi-gcc.