Skip to content

fix(libc): clear fopen_s output on invalid arguments - #1342

Closed
MarcZX10 wants to merge 1 commit into
boykopovar:mainfrom
MarcZX10:fix/libc-fopen-s-null-output
Closed

MarcZX10 wants to merge 1 commit into
boykopovar:mainfrom
MarcZX10:fix/libc-fopen-s-null-output

Conversation

@MarcZX10

@MarcZX10 MarcZX10 commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

fopen_s_nid_postfix now clears a valid result pointer before checking filename and mode. This keeps *result null on those runtime-constraint failures and on open failure, while a null result pointer returns EINVAL without dereferencing it.

Validation: cmake --build build --target guest_fiopen_tests -j2; ctest --test-dir build -R '^guest_(fiopen|working_directory|standard_streams|filesystem)$' --repeat until-fail:3 --output-on-failure; python3 tools/check_conventions.py --base upstream/main --head HEAD. Not tested: Windows or PS5 hardware/title runtime.

This push and PR are authorized by the user for this reviewed commit.

🤖 Generated with Claude Code

@github-actions github-actions Bot added the libc label Oct 8, 2026

@Dextroid17 Dextroid17 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Built and ran it, with a bisect to show the new test actually bites.

Linux (CachyOS, GCC, Release):

cmake --build build --target guest_fiopen_tests --parallel 16
ctest -R ^guest_fiopen$                                  →  1/1 Passed

Mutation check — the parent's FileAndHeap.cpp with your test kept:

pr-1342   (sets *result):  100% passed
pr-1342^  (parent libc):   Fiopen check failed at line 74   →  Failed

So the test discriminates and the store is what turns it green.

The order of the two checks is the correct one, not just a working one. C11 K.3.5.2.1 requires fopen_s to set *streamptr to a null pointer when the call fails, and a null filename or mode is a failure — so nulling before validating those arguments covers the cases the standard names, where nulling only on the I/O-failure path would not. Keeping if (!result) return GuestEinval; ahead of the store is the part a reader checks first, since it is the one ordering that would dereference a null pointer; as written the store is unreachable through a null result.

The test's cases are the right ones. The null-filename and null-mode cases cover validation, and the missing-file case covers an errno failure (ENOENT) rather than another validation path — a partial fix that nulled only in the argument branch would pass the first three assertions and fail that one. Using &_Stdout_nid_postfix as the incoming sentinel is also the correct way to write it: a fresh variable would satisfy a null check even if the function never wrote to it, whereas a sentinel proves the pointer was overwritten. That is the distinction between testing the behaviour and testing the absence of undefined behaviour.

I checked for the same rule elsewhere in libc and found nothing to follow up. No other _s function there takes an out-pointer of this shape, and freopen_s / tmpfile_s are not implemented, so there is no sibling with the same C11 obligation waiting to be missed.

This is a comment, not an approval — I have no merge rights here.

Written with AI assistance.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Same as #1336, for fopen_s leaving *result set on bad arguments.

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.

3 participants