fix(libc): leave a stream that is reading untouched in fflush on Linux #4372

Merged
milescoviello merged 1 commit from upstream-pr/4372 into main 2026-10-12 18:22:17 +00:00
milescoviello commented 2026-10-12 08:01:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T08:01:29Z.


What

fflush() passes every stream to glibc's fflush(). For a stream that is reading, glibc seeks the descriptor back to the stream position and drops its read buffer, and when a character has been pushed back it gets that position wrong. On main, after fgetc() returns 'a' from a file holding "abcdef\n" and ungetc('Z'), fflush() returns 0 and the next fread() returns "bcdef\n\n": the pushed-back 'Z' is gone and the last byte is read twice. The same happens for an "r+" stream whose last operation was a read.

FreeBSD's fflush() only writes out a stream that is writing and leaves every other stream as it is, lib/libc/stdio/fflush.c:

if ((fp->_flags & (__SWR | __SRW)) == 0)
	retval = 0;
else
	retval = __sflush(fp);

and __sflush():

t = fp->_flags;
if ((t & __SWR) == 0)
	return (0);

so the read gives "Zbcdef\n".

On Linux, fflush() now returns 0 without calling the host for a stream whose last operation was not a write (__fwriting() is 0). fflush(NULL) and streams that are writing go to the host as before. Windows is unchanged. No title is claimed.

Tested

Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags.

  • guest_standard_streams now opens a file holding "abcdef\n" with "r" and with "r+", reads 'a', pushes back 'Z', calls fflush() and checks that fread() returns "Zbcdef\n".
  • With only the test applied to main's library sources it fails:
    fflush of a "r" stream that is reading changed the data read next: "bcdef\n\n" (7 bytes), expected "Zbcdef\n" (7 bytes)
    
  • With the fix it passes, as do file_streams (which flushes streams that are writing), file_position, guest_fiopen and guest_filesystem, and all 299 guest_* tests pass (-E '^guest_video_out_').

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4826576533 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4372). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T08:01:29Z. --- ### What `fflush()` passes every stream to glibc's `fflush()`. For a stream that is reading, glibc seeks the descriptor back to the stream position and drops its read buffer, and when a character has been pushed back it gets that position wrong. On main, after `fgetc()` returns `'a'` from a file holding `"abcdef\n"` and `ungetc('Z')`, `fflush()` returns 0 and the next `fread()` returns `"bcdef\n\n"`: the pushed-back `'Z'` is gone and the last byte is read twice. The same happens for an `"r+"` stream whose last operation was a read. FreeBSD's `fflush()` only writes out a stream that is writing and leaves every other stream as it is, [`lib/libc/stdio/fflush.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/stdio/fflush.c#L75-L78): ```c if ((fp->_flags & (__SWR | __SRW)) == 0) retval = 0; else retval = __sflush(fp); ``` and [`__sflush()`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/stdio/fflush.c#L109-L111): ```c t = fp->_flags; if ((t & __SWR) == 0) return (0); ``` so the read gives `"Zbcdef\n"`. On Linux, `fflush()` now returns 0 without calling the host for a stream whose last operation was not a write (`__fwriting()` is 0). `fflush(NULL)` and streams that are writing go to the host as before. Windows is unchanged. No title is claimed. ### Tested Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags. - `guest_standard_streams` now opens a file holding `"abcdef\n"` with `"r"` and with `"r+"`, reads `'a'`, pushes back `'Z'`, calls `fflush()` and checks that `fread()` returns `"Zbcdef\n"`. - With only the test applied to main's library sources it fails: ```text fflush of a "r" stream that is reading changed the data read next: "bcdef\n\n" (7 bytes), expected "Zbcdef\n" (7 bytes) ``` - With the fix it passes, as do `file_streams` (which flushes streams that are writing), `file_position`, `guest_fiopen` and `guest_filesystem`, and all 299 `guest_*` tests pass (`-E '^guest_video_out_'`). ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xXPBSXx commented 2026-10-12 11:51:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T11:51:23Z.


Windows test: head 1337bd7e4 merged onto current main 3d8ffeb27 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j4: full build 0 errors; ctest with the filter guest_standard_stream: guest_standard_streams passed (1 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. The title says the fix is for Linux, so this Windows run only shows that the change builds and the stream tests still pass on Windows; I did not run the new tests against main without the change.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6123696778 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4372#issuecomment-6123696778). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T11:51:23Z. --- Windows test: head `1337bd7e4` merged onto current `main` `3d8ffeb27` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j4`: full build 0 errors; ctest with the filter `guest_standard_stream`: guest_standard_streams passed (1 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. The title says the fix is for Linux, so this Windows run only shows that the change builds and the stream tests still pass on Windows; I did not run the new tests against `main` without the change. AI-assisted (Claude Code).
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4372
No description provided.