fix(libc): reject incompatible fdopen access modes on Windows #4728

Merged
Ondra97 merged 1 commit from upstream-pr/4728 into main 2026-10-14 06:06:55 +00:00
Ondra97 commented 2026-10-12 19:53:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ondra97 on 2026-10-12T19:53:29Z.


What

On Windows, UCRT _fdopen accepts descriptors whose access does not match the requested stream mode. Validate the existing descriptor before wrapping it: incompatible modes return EINVAL, closed descriptors return EBADF, and rejection retains the descriptor and its state. The reference is FreeBSD 11.4 fdopen.c, recorded in libc debt.

Reuse the existing Windows handle/access query used by fcntl through internal static-inline libc helpers. Perform the validation before main's AppendThroughDescriptor, which can replace the descriptor and enable append mode. Preserve the upstream append behavior and its regression tests.

Tested

Windows, MinGW-w64 GCC 15.2.0, Release, BUILD_TESTING=ON, rebased on main c13dc7e6b.

The original access/mode regression failed before the fix. Full project build exits 0 and 14/14 relevant tests pass (1.40 s): standard streams, fcntl, file streams/position, dup, scalar/vector I/O, patched libraries and all six file-lock cases. The standard-stream test includes main's append checks and the 18 access/mode combinations plus a closed descriptor.

Rejected modes preserve the underlying Windows handle, file offset and contents. A raw write after rejecting write-only a+ must overwrite at offset 2 and advance to 3, detecting an accidental append flag even if the wrapped stream was rejected. The POSIX side also checks the original descriptor flags; Linux execution was not run here.

Patched/unpatched libc and libkernel PE export audits find no WindowsDescriptor helper/callback/state exports. git diff --check and the committed branch/PR conventions check pass. No game or PS5 console was run; no performance improvement is claimed.

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 under Silent stubs in their library's technical debt file
  • 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. — Codex
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4834301097 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4728). Originally posted by [Ondra97](https://github.com/Ondra97) on 2026-10-12T19:53:29Z. --- ### What On Windows, UCRT `_fdopen` accepts descriptors whose access does not match the requested stream mode. Validate the existing descriptor before wrapping it: incompatible modes return EINVAL, closed descriptors return EBADF, and rejection retains the descriptor and its state. The reference is FreeBSD 11.4 `fdopen.c`, recorded in libc debt. Reuse the existing Windows handle/access query used by `fcntl` through internal static-inline libc helpers. Perform the validation before main's `AppendThroughDescriptor`, which can replace the descriptor and enable append mode. Preserve the upstream append behavior and its regression tests. ### Tested Windows, MinGW-w64 GCC 15.2.0, Release, BUILD_TESTING=ON, rebased on main c13dc7e6b. The original access/mode regression failed before the fix. Full project build exits 0 and 14/14 relevant tests pass (1.40 s): standard streams, fcntl, file streams/position, dup, scalar/vector I/O, patched libraries and all six file-lock cases. The standard-stream test includes main's append checks and the 18 access/mode combinations plus a closed descriptor. Rejected modes preserve the underlying Windows handle, file offset and contents. A raw write after rejecting write-only `a+` must overwrite at offset 2 and advance to 3, detecting an accidental append flag even if the wrapped stream was rejected. The POSIX side also checks the original descriptor flags; Linux execution was not run here. Patched/unpatched libc and libkernel PE export audits find no WindowsDescriptor helper/callback/state exports. `git diff --check` and the committed branch/PR conventions check pass. No game or PS5 console was run; no performance improvement is claimed. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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. — Codex
xXPBSXx commented 2026-10-13 03:55:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T03:55:21Z.


Windows test: head 40b311b61 merged onto current main 495bf91a0 (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:6137875421 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4728#issuecomment-6137875421). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-13T03:55:21Z. --- Windows test: head `40b311b61` merged onto current `main` `495bf91a0` (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).
github-actions[bot] commented 2026-10-13 11:25:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T11:25:06Z.


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
#5092 docs/dev/debt/libc.md L4
#5632 core/libs/tests/GuestStandardStreams.cpp L431-432
#5668 core/libs/tests/GuestStandardStreams.cpp L431-432
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6144470694 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4728#issuecomment-6144470694). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T11:25:06Z. --- <!-- 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 | |---|---|---|---| | #5092 | `docs/dev/debt/libc.md` [L4](https://github.com/boykopovar/AnyPS5/blob/9013e89b112547922b95b5fc188ef0fc82e857bf/docs/dev/debt/libc.md#L4) | | | | #5632 | `core/libs/tests/GuestStandardStreams.cpp` [L431-432](https://github.com/boykopovar/AnyPS5/blob/9013e89b112547922b95b5fc188ef0fc82e857bf/core/libs/tests/GuestStandardStreams.cpp#L431-L432) | | | | #5668 | `core/libs/tests/GuestStandardStreams.cpp` [L431-432](https://github.com/boykopovar/AnyPS5/blob/9013e89b112547922b95b5fc188ef0fc82e857bf/core/libs/tests/GuestStandardStreams.cpp#L431-L432) | | |
adriadam10 commented 2026-10-13 15:34:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T15:34:32Z.


Checked the diff against FreeBSD fdopen.c semantics, the fcntl helper refactor (mechanical), the tests and the debt line; Linux build and ctest are green (217 tests). Windows behaviour is covered by the Windows build run before merge; merging once it is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6149196716 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4728#issuecomment-6149196716). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T15:34:32Z. --- Checked the diff against FreeBSD `fdopen.c` semantics, the fcntl helper refactor (mechanical), the tests and the debt line; Linux build and ctest are green (217 tests). Windows behaviour is covered by the Windows build run before merge; merging once it is green.
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!4728
No description provided.