Skip to content

libc/stdio: set the file offset on fflush() of a seekable read stream - #22241

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Shanks0224:libc-stdio-fflush
Nov 25, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Shanks0224:libc-stdio-fflush

Conversation

@Shanks0224

@Shanks0224 Shanks0224 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Note: Please adhere to Contributing Guidelines.

Summary

POSIX says that fflush() of a seekable input stream sets the file offset to
the 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() and fflush_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 the
build system, documentation or hardware.

Testing

Build host: Linux x86_64. Every case below gives the same result as glibc on
the host:

  • read stream, fread 3 bytes, fflush(): returns 0, offset 3;
  • "r+" stream with read data, fflush(): offset at the stream position;
  • "r+" file read with 12 fgetc() calls: correct data, same speed as master;
  • FIFO opened "r+", two fgetc(): a, b;
  • FIFO opened "r", fgetc(), fflush(f), fgetc(): no data lost;
  • stdin on a FIFO, getchar(), fflush(NULL), getchar(): x, y.

Known difference: after ungetc() and fflush(), the next byte is the one
after the pushed-back position (POSIX discards the pushback); glibc returns
the byte one further on.

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

Copy link
Copy Markdown
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.

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

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

esp32-devkitc

  • ROM: .flash.text +164 B (+0.1%, 133,564 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +164 B (+0.2%, 99,220 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +168 B (+0.2%, 86,532 B / 4,194,304 B, total: 2% used)

mps2-an500-citest

  • flash: .text +128 B (+0.1%, 247,408 B / 4,194,304 B, total: 6% used)

qemu-armv7a-citest

  • ROM: .data +16 B, .init_section -232 B, .text +232 B (+0.0%, 554,072 B / 1,048,576 B, total: 53% used)

qemu-armv8a

  • Code: .text.fflush +20 B (+0.1%, 377,704 B)

qemu-intel64

  • Code: .text +264 B (+0.0%, 8,707,155 B)
  • Data: .rodata +32 B (+0.0%, 109,089 B)

rx65n-rsk2mb

  • ROM: .text +32 B (+0.0%, 92,780 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +528 B (+0.1%, 408,944 B)

No memory changes detected for:


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

@xiaoxiang781216
xiaoxiang781216 merged commit fd660a0 into apache:master Nov 25, 2026
58 checks passed
@Shanks0224
Shanks0224 deleted the libc-stdio-fflush 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 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.

4 participants