Repository navigation
libc/stdio: set the file offset on fflush() of a seekable read stream - #22241
Merged
Merged
Conversation
fflush() of a stream that was only opened for reading returned EBADF and left the file offset where the read-ahead had put it, so after fread(buf, 1, 3, f); fflush(f); lseek(fileno(f), 0, SEEK_CUR) returned the size of the buffered block instead of 3. An update stream holding read data was not changed either. POSIX says that fflush() of a seekable input stream sets the file offset to the stream position and discards the characters pushed back with ungetc(). Do that in fflush() and fflush_unlocked() only, and only when the file can seek. The read data of a pipe or a terminal is kept, because it could not be read again, and lib_fflush_unlocked(), which the read path calls before every read, is not changed, so reading an "r+" stream does not discard its buffer. fflush() of a read stream now returns 0 instead of EBADF. Signed-off-by: ganjing <[email protected]>
Shanks0224
requested review from
jerpelea,
pussuw and
xiaoxiang781216
as code owners
November 24, 2026 05:41
Contributor
Author
|
@casaroli @jerpelea @xiaoxiang781216 could you review? This is the fflush() change split from #22179. It addresses both fflush comments from #22179: the read data is discarded only by an explicit fflush() and only when the file can seek, so "r+" reads, FIFOs and fflush(NULL) on a pipe stdin behave as on master and glibc. |
|
xiaoxiang781216
approved these changes
Nov 24, 2026
GUIDINGLI
approved these changes
Nov 24, 2026
jerpelea
approved these changes
Nov 24, 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
POSIX says that
fflush()of a seekable input stream sets the file offset tothe stream position and discards characters pushed back with
ungetc().NuttX returned EBADF and left the offset where the read-ahead put it. Do this
in
fflush()andfflush_unlocked()only, and only when the file can seek.Pipes and terminals keep their read data, and the internal flush that the read
path calls before every read is unchanged. Split from #22179.
Impact
fflush()of a read stream now returns 0 instead of EBADF. No impact on thebuild system, documentation or hardware.
Testing
Build host: Linux x86_64. Every case below gives the same result as glibc on
the host:
fread3 bytes,fflush(): returns 0, offset 3;fflush(): offset at the stream position;fgetc()calls: correct data, same speed as master;fgetc(): a, b;fgetc(),fflush(f),fgetc(): no data lost;getchar(),fflush(NULL),getchar(): x, y.Known difference: after
ungetc()andfflush(), the next byte is the oneafter the pushed-back position (POSIX discards the pushback); glibc returns
the byte one further on.