Repository navigation
Conversation
Dextroid17
left a comment
There was a problem hiding this comment.
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.
|
Same as #1336, for |
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