Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Ondra97:codex/fix-fdopen-access-mode
Oct 14, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Ondra97:codex/fix-fdopen-access-mode

Conversation

@Ondra97

@Ondra97 Ondra97 commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

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

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

@Ondra97
Ondra97 force-pushed the codex/fix-fdopen-access-mode branch from 40b311b to f2120a6 Compare October 13, 2026 03:41
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 13, 2026
@xXPBSXx

xXPBSXx commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

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

github-actions Bot commented Oct 13, 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
#5092 docs/dev/debt/libc.md L4
#5632 core/libs/tests/GuestStandardStreams.cpp L431-432
#5668 core/libs/tests/GuestStandardStreams.cpp L431-432

@adriadam10

Copy link
Copy Markdown
Collaborator

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.

@oneandonlydean
oneandonlydean merged commit 6546a33 into boykopovar:main Oct 14, 2026
14 of 15 checks passed
@Ondra97
Ondra97 deleted the codex/fix-fdopen-access-mode branch October 17, 2026 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants