fix(libc): leave a stream that is reading untouched in fflush on Linux #4372
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4372"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T08:01:29Z.
What
fflush()passes every stream to glibc'sfflush(). 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, afterfgetc()returns'a'from a file holding"abcdef\n"andungetc('Z'),fflush()returns 0 and the nextfread()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:and
__sflush():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_streamsnow opens a file holding"abcdef\n"with"r"and with"r+", reads'a', pushes back'Z', callsfflush()and checks thatfread()returns"Zbcdef\n".file_streams(which flushes streams that are writing),file_position,guest_fiopenandguest_filesystem, and all 299guest_*tests pass (-E '^guest_video_out_').Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T11:51:23Z.
Windows test: head
1337bd7e4merged onto currentmain3d8ffeb27(clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release,-j4: full build 0 errors; ctest with the filterguest_standard_stream: guest_standard_streams passed (1 test(s), 0 failed);python tools/check_conventions.py --base upstream/mainpassed. 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 againstmainwithout the change.AI-assisted (Claude Code).