fix(libc): append through a descriptor opened by fdopen in an append mode on Windows #4738
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4738"
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-12T20:12:18Z.
What
Fixes #4735. Carries @DavidSamir's fix branch from that issue (
DavidSamir:fix/libc-fdopen-append-windows,ca4e5910), keeping the commit and its authorship; it cherry-picks onto currentmainwithout changes.On Windows a stream made with
fdopen(fd, "a")(also"ab","a+","ae") wrote at the descriptor's current offset and overwrote what was there, because the C runtime's_fdopendoes not turn on append for an existing descriptor; a program that wraps its log or save file this way replaced the start of the file on each run. FreeBSD'sfdopen(lib/libc/stdio/fdopen.c) handles exactly this: "If opened for appending, but underlying descriptor does not have O_APPEND bit set, assert __SAPP so that __swrite() caller will _sseek() to the end before write." Before_fdopen, the append modes now reopen a duplicate of the descriptor's handle with_O_APPENDand move it onto the same descriptor number with_dup2, so the number, the file object, its offset, locks and sharing are kept and the C runtime seeks to the end before each write. Descriptors 0 to 2 and descriptors that are not disk files are left as they are. Linux is unchanged.guest_standard_streamsgainsCheckDescriptorAppend: for"a","ab","a+"and"ae", a descriptor on a file is moved to offset 2 and wrapped; a byte written and flushed, and one more written afterfseek(0, SEEK_SET), must both land at the end, andfilenomust still be the descriptor.Tested
main'sFileAndHeap.cppthe new check fails and with the fixguest_standard_streamspasses 5/5; probes of 313, 884 and 122 lines over the stream and descriptor functions differ frommainonly in the append contents; eleven catalog titles that importfdopenend the same way with both sets of libraries; fullctest995/995; no measurable cost.main01422156.guest_standard_streams,guest_filesystemandguest_kernel_fcntlpass, and all 312guest_*tests pass in two runs. The new check also passes withmain'sFileAndHeap.cpp, as expected since only the Windows path changes. I could not run Windows myself.tools/check_conventions.py --base upstream/mainpasses. The issue notes that #4728 adds lines at the same places, so whichever lands second needs a small rebase.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 github-actions[bot] on 2026-10-12T20:22:11Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libc/src/FileAndHeap.cppL198core/libs/tests/GuestStandardStreams.cppL394-395Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T00:10:16Z.
Windows test: head
dbbd6d582merged onto currentmain056d17ecc(clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release,-j2: 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. I did not run the new tests againstmainwithout the change.AI-assisted (Claude Code).