Skip to content

fix(libc): append through a descriptor opened by fdopen in an append mode on Windows - #4738

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:carry/fdopen-append-windows-4735
Oct 13, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:carry/fdopen-append-windows-4735

Conversation

@milescoviello

Copy link
Copy Markdown
Contributor

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 current main without 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 _fdopen does 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's fdopen (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_APPEND and 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_streams gains CheckDescriptorAppend: 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 after fseek(0, SEEK_SET), must both land at the end, and fileno must still be the descriptor.

Tested

  • From the issue: Windows 11, WinLibs MinGW-w64 GCC 15.2.0, Release: with main's FileAndHeap.cpp the new check fails and with the fix guest_standard_streams passes 5/5; probes of 313, 884 and 122 lines over the stream and descriptor functions differ from main only in the append contents; eleven catalog titles that import fdopen end the same way with both sets of libraries; full ctest 995/995; no measurable cost.
  • Linux, added for this carry: Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on main 0142215. guest_standard_streams, guest_filesystem and guest_kernel_fcntl pass, and all 312 guest_* tests pass in two runs. The new check also passes with main's FileAndHeap.cpp, as expected since only the Windows path changes. I could not run Windows myself.
  • tools/check_conventions.py --base upstream/main passes. The issue notes that fix(libc): reject incompatible fdopen access modes on Windows #4728 adds lines at the same places, so whichever lands second needs a small rebase.

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

@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

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.

Pull request Conflicting files (lines on main) Same exports Same new files
#4728 core/libs/prx/libc/src/FileAndHeap.cpp L198
core/libs/tests/GuestStandardStreams.cpp L394-395

@xXPBSXx

xXPBSXx commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Windows test: head dbbd6d582 merged onto current main 056d17ecc (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j2: 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. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

@oneandonlydean oneandonlydean self-assigned this Oct 13, 2026
@oneandonlydean
oneandonlydean merged commit b859349 into boykopovar:main Oct 13, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libc: fdopen in an append mode overwrites instead of appending on Windows; fix branch ready

4 participants