Repository navigation
fix(libc): fail fputs, fputc and fprintf on a closed stream as a PS5 does - #22047
oneandonlydean merged 3 commits into
Conversation
…does A write to a stream that fclose or a failed freopen closed set errno to EBADF in fputs, fputc and fprintf. On a PS5 fputs and fputc return -1 and leave errno unchanged, and fprintf fails with EINVAL (boykopovar#9763). Those three now do the same; putc, putchar and __swbuf forward to fputc and follow it. The other stream writes, which were not measured, keep EBADF. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PrivacyOnion
left a comment
There was a problem hiding this comment.
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 117239034b:
- Conventions:
tools/check_conventions.pypassed. - Build:
guest_freopen_host_standard_tests,guest_standard_stream_testsbuilt cleanly. - CTest (
guest_standard_streams,guest_standard_streams_closed_stdout,guest_standard_streams_exclusive_stdout,guest_standard_streams_reopen_closed_stdout,guest_standard_streams_update_stdout,guest_freopen_host_standard):1/6 Test #1253: guest_standard_streams_closed_stdout .......... Passed 0.02 sec 2/6 Test #1255: guest_standard_streams_reopen_closed_stdout ... Passed 0.04 sec 3/6 Test #1254: guest_standard_streams_exclusive_stdout ....... Passed 0.02 sec 4/6 Test #1256: guest_standard_streams_update_stdout .......... Passed 0.02 sec 5/6 Test #1260: guest_freopen_host_standard ................... Passed 0.02 sec 6/6 Test #1252: guest_standard_streams ........................ Passed 0.22 sec 100% tests passed, 0 tests failed out of 6 - Negative control: with
core/libs/prx/libc/src/FileAndHeap.cpp,core/libs/prx/libc/src/Formatting.cpp,core/libs/prx/libc/src/StandardStreams.cppreverted tomainand the new tests kept, the suite fails:Restoring the change makes it pass again.1/6 Test #1255: guest_standard_streams_reopen_closed_stdout ... Passed 0.02 sec 2/6 Test #1252: guest_standard_streams ........................Subprocess aborted***Exception: 0.42 sec 3/6 Test #1260: guest_freopen_host_standard ...................Subprocess aborted***Exception: 0.48 sec 4/6 Test #1254: guest_standard_streams_exclusive_stdout ....... Passed 0.02 sec 5/6 Test #1256: guest_standard_streams_update_stdout .......... Passed 0.02 sec 6/6 Test #1253: guest_standard_streams_closed_stdout ..........Subprocess aborted***Exception: 0.45 sec 50% tests passed, 3 tests failed out of 6 - Code reading:
- Verified core/libs/prx/libc/src/FileAndHeap.cpp fputs_nid_postfix correctly returns -1 without setting errno when stream is non-null and closed (!stream->IsOpen()).
- Verified core/libs/prx/libc/src/StandardStreams.cpp fputc_nid_postfix correctly returns -1 without modifying errno when stream is closed (!stream->IsOpen()), and that forwarding functions putc, putchar, and __swbuf follow this behavior.
- Verified core/libs/prx/libc/src/Formatting.cpp fprintf_nid_postfix correctly sets errno = 22 (EINVAL) and returns -1 before argument parsing or buffer formatting when stream is closed.
- Verified technical debt documentation fragment in docs/dev/debt/changes/fix-libc-closed-stream-write-errors.md conforms to AGENTS.md conventions and accurately documents the measured console behavior.
- Verified guest_standard_streams and guest_freopen_host_standard test suites verify that errno remains untouched (errno == 77) across fputs/fputc on closed streams and is set to 22 on fprintf.
Looks Good To Me
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — no blocking findings
The changed behavior is consistent with the stated closed-stream errno results in the inspected implementation and tests.
What I ran
Reviewed head 117239034be43795aa9f157cb58a35e87de74dcc. No commands or tests were run; the reader exposes file reads only.
Results
| Check | Result | Note |
|---|---|---|
| Diff and changed code | passed | Six changed files inspected |
| Tests | not run | Author-reported results only |
Findings
Blocking
None.
Non-blocking
None.
Not verified
The prepared bundle marks all six changed files as included, but reads using review/files/<path> were not served by the reader. I inspected the complete diff and targeted ranges in the three implementation files and GuestStandardStreams.cpp; full contents of GuestFreopenHostStandard.cpp and the debt note, and the rest of the changed files, were not read. files.json was not available. Tests and author-reported environment/results were not independently verified. The inspected files were FileAndHeap.cpp, Formatting.cpp, StandardStreams.cpp, and GuestStandardStreams.cpp; no files were excluded by rule according to the manifest.
To get this to a good state
Nothing is required.
|
Tested head Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release,
Whole batch: the full This is one combined tree, not a run of this PR alone. AI-assisted: yes (Claude Code) |
# Conflicts: # core/libs/prx/libc/src/FileAndHeap.cpp # core/libs/prx/libc/src/StandardStreams.cpp
…unchanged On a closed stream fputc returns -1 without setting errno, as measured on a PS5. The fcloseall test asserted EBADF. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Merged current
Tests on Linux (Debian 13, GCC 14.2, Release), all passing: |
|
No actionable comments were generated in the recent review. Walkthrough
Change: Bug fix Merge Risk:
|
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
The reviewed code changes the closed stream behavior of fputs, fputc, and fprintf and updates tests to assert the documented errno results; I found no verified blocking defect.
What I ran
Read the prepared diff and all six changed source and test files plus the new debt note at head 1d3137ca095f599251679fdb1825c3af71473cbf, using repo_read.py.
No commands or tests were run. The test and negative control results in the PR description are author reported.
Results
| Check | Result | Note |
|---|---|---|
| Diff and changed files | passed | All seven changed files read |
| Tests and negative control | not run | Results supplied by author |
| Cross platform behavior | not run | Windows and macOS not tested |
Findings
Blocking
None.
Non-blocking
None.
Not verified
I read every file listed in the manifest; none were excluded. I did not independently verify the PS5 measurements, run tests, or inspect callers beyond the changed functions and tests. The author reports Linux test results and says Windows and macOS were not tested.
To get this to a good state
Nothing is required.
|
The one red check, AI-assisted: yes. |
|
Update on the red macOS check: AI-assisted: yes. |
ligmaSec
left a comment
There was a problem hiding this comment.
Built the submitted stream and formatting sources and ran all three Linux fixtures. FileStreams, GuestFreopenHostStandard, and GuestStandardStreams passed. The new cases check the return and errno behavior for fputs, fputc and fprintf on closed streams.
AI-assisted: yes.
What
A write to a stream that
fcloseor a failedfreopenclosed seterrnotoEBADFinfputs,fputcandfprintf. On a PS5fputsandfputcreturn -1 and leaveerrnounchanged, andfprintffails withEINVAL(#9763; thefreopen of a closed streamentry indocs/dev/debt/libc.md).putc,putcharand__swbufforward tofputcand follow it.fwrite,puts,printfand the wide forms) were not measured and keepEBADF; the debt entry says so.GuestStandardStreams.cppandGuestFreopenHostStandard.cppcheck the measured results where they checkedEBADFbefore.Tested
Linux (Debian 13, GCC 14.2.0, Release), on
mainec7369ecb8.ctest -R '^guest_(standard_streams.*|freopen_host_standard)$': 6/6 passed.main's sources and this branch's tests,guest_standard_streams,guest_standard_streams_closed_stdoutandguest_freopen_host_standardfail (errno9).ctest(5896 tests,-j 12) with this branch and my other twelve branches of this round merged ontomain521fb7fec4: 5880 passed. The 16 failures are the set that fails onmainin the same environment: sixagc_drivercases (agc_driver_image_table_packed_guardspasses when run on its own) and ten that need a.gitdirectory, network or a passwd entry for the user.python3 tools/check_conventions.py --base origin/main: passed.python3 tools/pr_overlap.py --branch: failed closed on feat(libc): support archive-backed stdio reads #7447, fix(libc): support character reads from random device streams #8150, fix(libc): follow snprintf_s and sscanf_s constraints as measured on a PS5 #9087, feat(libc): decode non-ASCII scanset members and non-BMP characters in the guest wide scanf #9340, feat(libc): add std::__sce_v2::_Random_device, _Fiopen and setw #9526, fix(libc): bound narrow formatting fields #11332, feat(libc): implement __std_calloc_crt allocation wrapper #12184, feat(libkernel): cancel blocked POSIX reads with owned operations #12682, fix(libkernel): preserve blocking sockets over nonblocking native transport #12688, feat(libkernel): cancel blocked POSIX writes with partial progress #12700 and feat(libkernel): support deferred cancellation in POSIX accept #12965, which conflict withmain; none of their diffs touches the closed-stream checks offputs,fputcorfprintf.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Summary by CodeRabbit