fix(libc): fail printf_s on a closed standard output with EBADF #10781

Merged
MarcZX10 merged 2 commits from upstream-pr/10781 into main 2026-10-26 20:04:41 +00:00
MarcZX10 commented 2026-10-25 00:31:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:31:26Z.


What

printf_s on a guest stdout that a failed freopen has closed threw FileStream: closed stream out of the export. It now returns -1 with errno 9 (EBADF), as printf, puts and vprintf already do on a closed stdout. Two commits: fix, test.

Open PR #9087 changes one line of printf_s for the Annex K constraints; the two changes are independent.

Tested

Rebase follow-up (2026-10-25): rebased onto current upstream main e7e3e9c1984c293f06bff08428b8c89b7b932ac2 without conflicts. Its combined merge with #10766 is also clean; the two changes touch separate test cases and are independent. On the rebased head, GCC 12.2 C++20 -Wall -Wextra -Werror syntax checks passed for Formatting.cpp and GuestStandardStreams.cpp; git diff --check and tools/check_conventions.py --base upstream/main passed. The full local CMake test build could not configure because the SDL2 CMake package is unavailable, so no post-rebase runtime test is claimed. The runtime evidence below is from the prior base.

Linux x86-64 (Debian 12, GCC 12), Ninja Release. The extended guest_standard_streams_closed_stdout case checks printf_s("%d", 1) and printf_s("%p", nullptr) on a closed stdout: it fails on current main with an uncaught runtime_error and passes with the fix. python3 tools/check_conventions.py --base upstream/main --head HEAD and git diff --check upstream/main pass. Windows, macOS and hardware were not run.

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 (not applicable; no third-party code added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5000469492 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10781). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T00:31:26Z. --- ### What `printf_s` on a guest stdout that a failed `freopen` has closed threw `FileStream: closed stream` out of the export. It now returns -1 with errno 9 (EBADF), as `printf`, `puts` and `vprintf` already do on a closed stdout. Two commits: fix, test. Open PR #9087 changes one line of `printf_s` for the Annex K constraints; the two changes are independent. ### Tested Rebase follow-up (2026-10-25): rebased onto current upstream `main` `e7e3e9c1984c293f06bff08428b8c89b7b932ac2` without conflicts. Its combined merge with #10766 is also clean; the two changes touch separate test cases and are independent. On the rebased head, GCC 12.2 C++20 `-Wall -Wextra -Werror` syntax checks passed for `Formatting.cpp` and `GuestStandardStreams.cpp`; `git diff --check` and `tools/check_conventions.py --base upstream/main` passed. The full local CMake test build could not configure because the SDL2 CMake package is unavailable, so no post-rebase runtime test is claimed. The runtime evidence below is from the prior base. Linux x86-64 (Debian 12, GCC 12), Ninja Release. The extended `guest_standard_streams_closed_stdout` case checks `printf_s("%d", 1)` and `printf_s("%p", nullptr)` on a closed stdout: it fails on current `main` with an uncaught `runtime_error` and passes with the fix. `python3 tools/check_conventions.py --base upstream/main --head HEAD` and `git diff --check upstream/main` pass. Windows, macOS and hardware were not run. ### 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) - [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 (not applicable; no third-party code added) - [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
MarcZX10 commented 2026-10-25 02:11:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T02:11:35Z.

Review: COMMENTED


I reviewed the current head and ran the focused local checks available for this change. The review found no additional blocking issue; the recorded scope and test claims match the diff. CI is green for the jobs that ran; the host product jobs are skipped by the current workflow.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641099763 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10781#pullrequestreview-5641099763). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:11:35Z. **Review:** COMMENTED --- I reviewed the current head and ran the focused local checks available for this change. The review found no additional blocking issue; the recorded scope and test claims match the diff. CI is green for the jobs that ran; the host product jobs are skipped by the current workflow.
MarcZX10 commented 2026-10-25 02:12:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T02:12:52Z.

Review: COMMENTED


I checked the current implementation and tests for printf_s with closed stdout, including formatting and null pointer cases. Returning -1 with EBADF matches the neighboring closed-stream output functions. The relinker, ASan, register-context and write-watch CI jobs passed; product jobs are skipped. No additional blocking issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641106805 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10781#pullrequestreview-5641106805). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:12:52Z. **Review:** COMMENTED --- I checked the current implementation and tests for printf_s with closed stdout, including formatting and null pointer cases. Returning -1 with EBADF matches the neighboring closed-stream output functions. The relinker, ASan, register-context and write-watch CI jobs passed; product jobs are skipped. No additional blocking issue found.
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!10781
No description provided.