fix(libc): clear the orientation when freopen opens a closed stream #23948

Merged
ToRaNek merged 1 commit from upstream-pr/23948 into main 2026-11-20 00:40:54 +00:00
ToRaNek commented 2026-11-19 22:38:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ToRaNek on 2026-11-19T22:38:51Z.


What

A stream that was oriented while closed kept that orientation after a successful freopen. With a stream closed by fclose, fwide(stream, 1) (or fgetwc, fgetws, fwscanf, which orient the stream before they find it closed and fail with EBADF) sets the orientation; freopen(path, "w", stream) then succeeded, and fwide(stream, 0) on the new file still returned 1 instead of 0, and fwide(stream, -1) could no longer make it byte-oriented.

The fwide debt entry states the rule: "a successful freopen clears it". FileStream::Reopen applies it on the path for a stream that is still open (_orientation.store(0) before the host freopen), and Close and Detach clear it too. The path for a closed stream returned Adopt(open(filename, mode)) directly, and Adopt resets every other piece of guest-side state (error flag, random-device flag, pending low surrogate, push-back count) but not the orientation. This PR clears it there once the open succeeded.

A failed freopen of a closed stream is unchanged: it returns before Adopt and leaves the stream as it was.

Tested

Windows 11 IoT Enterprise LTSC 2024 (10.0.26100), WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON.

  • Three lines added to CheckClosedStreamReopen in core/libs/tests/GuestStandardStreams.cpp, which already closes a stream and reopens it: orient the closed stream, then after the successful freopen require fwide(stream, 0) == 0, and -1 after the following fputs.
  • Red: with the new test file on main 9b11f4281e and FileStream.hpp unchanged, guest_standard_streams aborts.
  • Green: with the fix, ctest -R "^guest_standard_streams|fwide" passes 6 of 6 (guest_fwide, guest_standard_streams and its four standard-output variants), built with the guest_standard_stream_tests and guest_fwide_tests targets.
  • After a full rebuild of the tree with the changed header, ctest -R "^guest_(standard_stream|fwide|file|wide|.*scan|.*format|stdio|freopen|fiopen|stream)|file_stream|^file_" passes 52 of 52.
  • python tools/check_conventions.py --base origin/main passes. No debt change: the entry already describes the behaviour this PR makes true.
  • python tools/pr_overlap.py --branch fix/libc-freopen-closed-stream-orientation reports #11831, which rewrites the same return Adopt(open(filename, mode)); line to own the replacement handle and does not touch the orientation; the two changes are independent and conflict only textually. It could not compare #7447, #12682, #12688, #12700 and #12965, which conflict with main on FileStream.hpp; compared by hand, none of them changes _orientation or that line.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5404181371 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23948). Originally posted by [ToRaNek](https://github.com/ToRaNek) on 2026-11-19T22:38:51Z. --- ### What A stream that was oriented while closed kept that orientation after a successful `freopen`. With a stream closed by `fclose`, `fwide(stream, 1)` (or `fgetwc`, `fgetws`, `fwscanf`, which orient the stream before they find it closed and fail with `EBADF`) sets the orientation; `freopen(path, "w", stream)` then succeeded, and `fwide(stream, 0)` on the new file still returned 1 instead of 0, and `fwide(stream, -1)` could no longer make it byte-oriented. The `fwide` debt entry states the rule: "a successful `freopen` clears it". `FileStream::Reopen` applies it on the path for a stream that is still open (`_orientation.store(0)` before the host `freopen`), and `Close` and `Detach` clear it too. The path for a closed stream returned `Adopt(open(filename, mode))` directly, and `Adopt` resets every other piece of guest-side state (error flag, random-device flag, pending low surrogate, push-back count) but not the orientation. This PR clears it there once the open succeeded. A failed `freopen` of a closed stream is unchanged: it returns before `Adopt` and leaves the stream as it was. ### Tested Windows 11 IoT Enterprise LTSC 2024 (10.0.26100), WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`. - Three lines added to `CheckClosedStreamReopen` in `core/libs/tests/GuestStandardStreams.cpp`, which already closes a stream and reopens it: orient the closed stream, then after the successful `freopen` require `fwide(stream, 0) == 0`, and `-1` after the following `fputs`. - Red: with the new test file on `main` `9b11f4281e` and `FileStream.hpp` unchanged, `guest_standard_streams` aborts. - Green: with the fix, `ctest -R "^guest_standard_streams|fwide"` passes 6 of 6 (`guest_fwide`, `guest_standard_streams` and its four standard-output variants), built with the `guest_standard_stream_tests` and `guest_fwide_tests` targets. - After a full rebuild of the tree with the changed header, `ctest -R "^guest_(standard_stream|fwide|file|wide|.*scan|.*format|stdio|freopen|fiopen|stream)|file_stream|^file_"` passes 52 of 52. - `python tools/check_conventions.py --base origin/main` passes. No debt change: the entry already describes the behaviour this PR makes true. - `python tools/pr_overlap.py --branch fix/libc-freopen-closed-stream-orientation` reports #11831, which rewrites the same `return Adopt(open(filename, mode));` line to own the replacement handle and does not touch the orientation; the two changes are independent and conflict only textually. It could not compare #7447, #12682, #12688, #12700 and #12965, which conflict with `main` on `FileStream.hpp`; compared by hand, none of them changes `_orientation` or that line. ### 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) 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
PrivacyOnion commented 2026-11-19 22:46:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T22:46:45Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 36651ef7fd:

  • Conventions: tools/check_conventions.py passed.
  • Build: 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):
    1/5 Test #1357: guest_standard_streams_closed_stdout ..........   Passed    0.01 sec
    2/5 Test #1358: guest_standard_streams_exclusive_stdout .......   Passed    0.01 sec
    3/5 Test #1359: guest_standard_streams_reopen_closed_stdout ...   Passed    0.01 sec
    4/5 Test #1360: guest_standard_streams_update_stdout ..........   Passed    0.01 sec
    5/5 Test #1356: guest_standard_streams ........................   Passed    0.18 sec
    100% tests passed, 0 tests failed out of 5
    
  • Negative control: with core/libs/prx/libc/include/FileStream.hpp reverted to main and the new tests kept, the suite fails:
    1/5 Test #1359: guest_standard_streams_reopen_closed_stdout ...   Passed    0.01 sec
    2/5 Test #1360: guest_standard_streams_update_stdout ..........   Passed    0.01 sec
    3/5 Test #1358: guest_standard_streams_exclusive_stdout .......   Passed    0.01 sec
    4/5 Test #1357: guest_standard_streams_closed_stdout ..........   Passed    0.01 sec
    5/5 Test #1356: guest_standard_streams ........................Subprocess aborted***Exception:   0.20 sec
    80% tests passed, 1 tests failed out of 5
    
    Restoring the change makes it pass again.
  • Code reading:
    • Checked FileStream::Reopen in core/libs/prx/libc/include/FileStream.hpp: verified that when reopening a closed stream (!_handle), after Adopt(open(filename, mode)) succeeds, _orientation.store(0, std::memory_order_relaxed) resets orientation to unoriented (0) matching POSIX/C standard behavior.
    • Checked failure paths in FileStream::Reopen: if Adopt or open fails, the function returns false early without altering the stream state or orientation.
    • Checked existing FileStream::Reopen open-stream path: verified that it already resets _orientation.store(0, std::memory_order_relaxed) and that Detach() also clears orientation.
    • Checked test additions in core/libs/tests/GuestStandardStreams.cpp: verified that CheckClosedStreamReopen orients the closed stream with fwide(1), checks fwide==0 immediately after freopen, and checks fwide==-1 after byte output (fputs).
    • Checked AGENTS.md compliance: conventions check passed, no comments added in code, no fake stubs or special-casing introduced.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5984648330 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23948#pullrequestreview-5984648330). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-19T22:46:45Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `36651ef7fd`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `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`): ``` 1/5 Test #1357: guest_standard_streams_closed_stdout .......... Passed 0.01 sec 2/5 Test #1358: guest_standard_streams_exclusive_stdout ....... Passed 0.01 sec 3/5 Test #1359: guest_standard_streams_reopen_closed_stdout ... Passed 0.01 sec 4/5 Test #1360: guest_standard_streams_update_stdout .......... Passed 0.01 sec 5/5 Test #1356: guest_standard_streams ........................ Passed 0.18 sec 100% tests passed, 0 tests failed out of 5 ``` - **Negative control**: with `core/libs/prx/libc/include/FileStream.hpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/5 Test #1359: guest_standard_streams_reopen_closed_stdout ... Passed 0.01 sec 2/5 Test #1360: guest_standard_streams_update_stdout .......... Passed 0.01 sec 3/5 Test #1358: guest_standard_streams_exclusive_stdout ....... Passed 0.01 sec 4/5 Test #1357: guest_standard_streams_closed_stdout .......... Passed 0.01 sec 5/5 Test #1356: guest_standard_streams ........................Subprocess aborted***Exception: 0.20 sec 80% tests passed, 1 tests failed out of 5 ``` Restoring the change makes it pass again. - **Code reading**: - Checked FileStream::Reopen in core/libs/prx/libc/include/FileStream.hpp: verified that when reopening a closed stream (!_handle), after Adopt(open(filename, mode)) succeeds, _orientation.store(0, std::memory_order_relaxed) resets orientation to unoriented (0) matching POSIX/C standard behavior. - Checked failure paths in FileStream::Reopen: if Adopt or open fails, the function returns false early without altering the stream state or orientation. - Checked existing FileStream::Reopen open-stream path: verified that it already resets _orientation.store(0, std::memory_order_relaxed) and that Detach() also clears orientation. - Checked test additions in core/libs/tests/GuestStandardStreams.cpp: verified that CheckClosedStreamReopen orients the closed stream with fwide(1), checks fwide==0 immediately after freopen, and checks fwide==-1 after byte output (fputs). - Checked AGENTS.md compliance: conventions check passed, no comments added in code, no fake stubs or special-casing introduced. Looks Good To Me
MarcZX10 commented 2026-11-19 23:37:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T23:37:04Z.

Review: APPROVED


Reviewed — looks good

The change clears the stream orientation after a successful reopen of a closed stream, and the inspected code and focused regression test support the stated behavior.

What I ran

Reviewed head 36651ef7fd2f9af493a6d122c8e07b832f0c09e5. No commands or tests were run in this review; the test results below are author-reported.

Results

Check Result Note
Source review passed Both changed files read
Tests not run Results supplied by the author

Findings

Blocking

None.

Non-blocking

None.

Not verified

Builds and tests were not run during this review. The author reports the focused suite passed 6/6 and a broader suite passed 52/52 on Windows 11 IoT Enterprise LTSC 2024 with WinLibs GCC 15.2.0 r7. No files were excluded; both files listed as reviewed in the manifest were read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5985145993 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23948#pullrequestreview-5985145993). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-19T23:37:04Z. **Review:** APPROVED --- # Reviewed — looks good The change clears the stream orientation after a successful reopen of a closed stream, and the inspected code and focused regression test support the stated behavior. ## What I ran Reviewed head `36651ef7fd2f9af493a6d122c8e07b832f0c09e5`. No commands or tests were run in this review; the test results below are author-reported. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Both changed files read | | Tests | not run | Results supplied by the author | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Builds and tests were not run during this review. The author reports the focused suite passed 6/6 and a broader suite passed 52/52 on Windows 11 IoT Enterprise LTSC 2024 with WinLibs GCC 15.2.0 r7. No files were excluded; both files listed as reviewed in the manifest were read. ## To get this to a good state Nothing is required.
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!23948
No description provided.