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

Merged
milescoviello merged 1 commit from upstream-pr/4738 into main 2026-10-13 02:37:03 +00:00
milescoviello commented 2026-10-12 20:12:18 +00:00 (Migrated from github.com)

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 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 01422156. 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 #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

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4834482069 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4738). Originally posted by [milescoviello](https://github.com/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 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` 01422156. `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 #4728 adds lines at the same places, so whichever lands second needs a small rebase. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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)
github-actions[bot] commented 2026-10-12 20:22:11 +00:00 (Migrated from github.com)

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.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6131972437 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4738#issuecomment-6131972437). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T20:22:11Z. --- <!-- pr-overlap --> 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](https://github.com/boykopovar/AnyPS5/blob/834de1ab102b597a9e1b88cc5c62040aecdec849/core/libs/prx/libc/src/FileAndHeap.cpp#L198)<br>`core/libs/tests/GuestStandardStreams.cpp` [L394-395](https://github.com/boykopovar/AnyPS5/blob/834de1ab102b597a9e1b88cc5c62040aecdec849/core/libs/tests/GuestStandardStreams.cpp#L394-L395) | | |
xXPBSXx commented 2026-10-13 00:10:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T00:10:16Z.


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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6135062304 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4738#issuecomment-6135062304). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T00:10:16Z. --- 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).
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!4738
No description provided.