fix(libc): fail the wide calls on a closed stream with EBADF #10766

Merged
MarcZX10 merged 4 commits from upstream-pr/10766 into main 2026-10-25 07:41:53 +00:00
MarcZX10 commented 2026-10-24 23:30:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T23:30:58Z.


What

A stream that a failed freopen closed fails the narrow stdio calls with EBADF (#5632). The wide calls were left behind:

  • ungetwc reached GetHandle() and threw FileStream: closed stream out of the export, where ungetc returns -1 with errno 9.
  • fputwc (and putwc, putwchar) and fputws threw inside GetNativeStream, which they catch and report as -1 with errno 22, where fputc, fputs and vfwprintf set 9.
  • wprintf on a closed standard output did the same (errno 22), where printf, puts and vwprintf set 9.

Each now checks IsOpen() first and returns -1 with errno 9, like its narrow sibling. One commit per function group, then the test.

Textual overlap: #10324 adds Orientation calls at the top of the same functions. The two changes do not depend on each other.

Tested

Rebase follow-up (2026-10-25): rebased onto current upstream main e7e3e9c1984c293f06bff08428b8c89b7b932ac2 without conflicts. On the rebased head, GCC 12.2 C++20 -Wall -Wextra -Werror syntax checks passed for FormattingWide.cpp, StandardStreams.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 only (Debian 12, GCC 12, Release); nothing was run on Windows, macOS or a console.

  • guest_standard_streams, guest_standard_streams_closed_stdout and guest_standard_streams_exclusive_stdout with the new checks (fputwc, putwc, fputws, ungetwc on a closed stream; wprintf, putwchar on a closed stdout). On main the run stops at fputwc (errno 22 instead of 9), and ungetwc would throw. With this PR all three pass.
  • guest_wide_formatting, guest_wide_strings, guest_wide_conversion, guest_wide_stream_scan, file_streams, file_position pass.
  • tools/check_conventions.py --base upstream/main --head HEAD and git diff --check are clean.

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
  • 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:5000126202 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10766). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T23:30:58Z. --- ### What A stream that a failed `freopen` closed fails the narrow stdio calls with `EBADF` (#5632). The wide calls were left behind: - `ungetwc` reached `GetHandle()` and threw `FileStream: closed stream` out of the export, where `ungetc` returns -1 with `errno` 9. - `fputwc` (and `putwc`, `putwchar`) and `fputws` threw inside `GetNativeStream`, which they catch and report as -1 with `errno` 22, where `fputc`, `fputs` and `vfwprintf` set 9. - `wprintf` on a closed standard output did the same (`errno` 22), where `printf`, `puts` and `vwprintf` set 9. Each now checks `IsOpen()` first and returns -1 with `errno` 9, like its narrow sibling. One commit per function group, then the test. Textual overlap: #10324 adds `Orientation` calls at the top of the same functions. The two changes do not depend on each other. ### Tested Rebase follow-up (2026-10-25): rebased onto current upstream `main` `e7e3e9c1984c293f06bff08428b8c89b7b932ac2` without conflicts. On the rebased head, GCC 12.2 C++20 `-Wall -Wextra -Werror` syntax checks passed for `FormattingWide.cpp`, `StandardStreams.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 only (Debian 12, GCC 12, Release); nothing was run on Windows, macOS or a console. - `guest_standard_streams`, `guest_standard_streams_closed_stdout` and `guest_standard_streams_exclusive_stdout` with the new checks (`fputwc`, `putwc`, `fputws`, `ungetwc` on a closed stream; `wprintf`, `putwchar` on a closed stdout). On `main` the run stops at `fputwc` (`errno` 22 instead of 9), and `ungetwc` would throw. With this PR all three pass. - `guest_wide_formatting`, `guest_wide_strings`, `guest_wide_conversion`, `guest_wide_stream_scan`, `file_streams`, `file_position` pass. - `tools/check_conventions.py --base upstream/main --head HEAD` and `git diff --check` are clean. ### Checklist - [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 - [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:36 +00:00 (Migrated from github.com)

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

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:5641099810 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10766#pullrequestreview-5641099810). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:11:36Z. **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:45 +00:00 (Migrated from github.com)

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

Review: COMMENTED


I checked the current diff against the reported closed-stream behavior and confirmed it applies the EBADF path consistently across the wide output and input calls listed. The separate #10324 orientation changes are composable. CI passed for the relinker, ASan, register-context and write-watch jobs; the dedicated product jobs are skipped. No additional blocking issue found.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641106081 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10766#pullrequestreview-5641106081). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:12:45Z. **Review:** COMMENTED --- I checked the current diff against the reported closed-stream behavior and confirmed it applies the EBADF path consistently across the wide output and input calls listed. The separate #10324 orientation changes are composable. CI passed for the relinker, ASan, register-context and write-watch jobs; the dedicated 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!10766
No description provided.