fix(libc): fail fputs, fputc and fprintf on a closed stream as a PS5 does #22047

Merged
brajankw merged 3 commits from upstream-pr/22047 into main 2026-12-03 19:29:49 +00:00
brajankw commented 2026-11-17 03:06:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by brajankw on 2026-11-17T03:06:28Z.


What

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 (#9763; the freopen of a closed stream entry in docs/dev/debt/libc.md).

  • Those three now do the same; putc, putchar and __swbuf forward to fputc and follow it.
  • The other stream writes (fwrite, puts, printf and the wide forms) were not measured and keep EBADF; the debt entry says so.
  • GuestStandardStreams.cpp and GuestFreopenHostStandard.cpp check the measured results where they checked EBADF before.

Tested

Linux (Debian 13, GCC 14.2.0, Release), on main ec7369ecb8.

  • ctest -R '^guest_(standard_streams.*|freopen_host_standard)$': 6/6 passed.
  • Negative control: with main's sources and this branch's tests, guest_standard_streams, guest_standard_streams_closed_stdout and guest_freopen_host_standard fail (errno 9).
  • Full ctest (5896 tests, -j 12) with this branch and my other twelve branches of this round merged onto main 521fb7fec4: 5880 passed. The 16 failures are the set that fails on main in the same environment: six agc_driver cases (agc_driver_image_table_packed_guards passes when run on its own) and ten that need a .git directory, 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 #7447, #8150, #9087, #9340, #9526, #11332, #12184, #12682, #12688, #12700 and #12965, which conflict with main; none of their diffs touches the closed-stream checks of fputs, fputc or fprintf.
  • Not tested on Windows or macOS. No title.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Failed writes to closed streams now preserve the existing error value for select operations. Formatted output still sets an error when the stream cannot be opened.
  • Documentation
    • Added notes describing closed-stream behavior, including file reopening and write errors.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5349753438 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22047). Originally posted by [brajankw](https://github.com/brajankw) on 2026-11-17T03:06:28Z. --- ### What 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` (#9763; the `freopen of a closed stream` entry in `docs/dev/debt/libc.md`). - Those three now do the same; `putc`, `putchar` and `__swbuf` forward to `fputc` and follow it. - The other stream writes (`fwrite`, `puts`, `printf` and the wide forms) were not measured and keep `EBADF`; the debt entry says so. - `GuestStandardStreams.cpp` and `GuestFreopenHostStandard.cpp` check the measured results where they checked `EBADF` before. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Linux (Debian 13, GCC 14.2.0, Release), on `main` `ec7369ecb8`. - `ctest -R '^guest_(standard_streams.*|freopen_host_standard)$'`: 6/6 passed. - Negative control: with `main`'s sources and this branch's tests, `guest_standard_streams`, `guest_standard_streams_closed_stdout` and `guest_freopen_host_standard` fail (`errno` 9). - Full `ctest` (5896 tests, `-j 12`) with this branch and my other twelve branches of this round merged onto `main` `521fb7fec4`: 5880 passed. The 16 failures are the set that fails on `main` in the same environment: six `agc_driver` cases (`agc_driver_image_table_packed_guards` passes when run on its own) and ten that need a `.git` directory, 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 #7447, #8150, #9087, #9340, #9526, #11332, #12184, #12682, #12688, #12700 and #12965, which conflict with `main`; none of their diffs touches the closed-stream checks of `fputs`, `fputc` or `fprintf`. - Not tested on Windows or macOS. No title. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Failed writes to closed streams now preserve the existing error value for select operations. Formatted output still sets an error when the stream cannot be opened. * **Documentation** * Added notes describing closed-stream behavior, including file reopening and write errors. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
PrivacyOnion commented 2026-11-17 03:10:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T03:10:09Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 117239034b:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_freopen_host_standard_tests, guest_standard_stream_tests built 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.cpp reverted to main and the new tests kept, the suite fails:
    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
    
    Restoring the change makes it pass again.
  • 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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5932134649 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22047#pullrequestreview-5932134649). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T03:10:09Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `117239034b`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_freopen_host_standard_tests`, `guest_standard_stream_tests` built 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.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 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 ``` Restoring the change makes it pass again. - **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
MarcZX10 commented 2026-11-21 08:42:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T08:42:18Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6004201459 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22047#pullrequestreview-6004201459). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T08:42:18Z. **Review:** COMMENTED --- # 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.
MarcZX10 commented 2026-11-28 15:23:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T15:23:47Z.

Review: APPROVED


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6098682558 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22047#pullrequestreview-6098682558). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-28T15:23:47Z. **Review:** APPROVED --- # 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.
ligmaSec commented 2026-12-03 11:23:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T11:23:52Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6168778171 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22047#pullrequestreview-6168778171). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T11:23:52Z. **Review:** COMMENTED --- 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.
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!22047
No description provided.