fix(libc): grow the pushback room of Windows streams for repeated ungetc #17459

Merged
Danigamer05 merged 5 commits from upstream-pr/17459 into main 2026-11-20 20:17:20 +00:00
Danigamer05 commented 2026-11-08 19:55:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-08T19:55:09Z.


What

Fixes #17423: guest_standard_streams aborts on Windows on current main, because the getdelim read-error fixture from #12920 pushes back two bytes on an unbuffered stream and the second ungetc returns EOF.

ungetc and ungetwc forwarded to the host ungetc. The UCRT pushes back by moving its read pointer back inside the stream buffer, so it has no room once the pointer sits at the start of the buffer with unread bytes in it: on an unbuffered stream (a one-byte buffer) after one pushback, and on a buffered stream after one pushback into a buffer that a read just filled. FreeBSD keeps a separate pushback buffer that grows, so it accepts these calls.

On Windows, FileStream::PushBack now makes room before the host ungetc when there is none. Under the stream lock, it moves the unread bytes to the end of a buffer twice their size (at least 16 bytes more, and never smaller than the stream's buffer size), so the host ungetc can step back into it:

  • A buffered stream owns its buffer through the UCRT (_IOBUFFER_CRT). The new buffer replaces it, and the UCRT frees it as before. The buffer size the UCRT reads with does not change.
  • An unbuffered stream (_IOBUFFER_NONE) reads through its one-byte character buffer, which the UCRT never frees. The new buffer belongs to the FileStream, and the UCRT keeps reading one byte at a time into its start. The FileStream frees it on close, on reopen, on detaching a host standard stream and on destruction, first pointing the stream back at its character buffer if it still uses the new one.
  • A stream with a user buffer keeps the old behavior. setvbuf never hands the UCRT a user buffer, so no stream has one.

Linux and macOS keep calling the host ungetc, which already grows its pushback room.

Tested

Windows 11 Pro 26200, MinGW-w64 GCC 15.2 (UCRT), Release, on main 095ff72367c, and again merged onto main 59f3a64cf61. Not run on Linux or macOS: the change is Windows only, so the new check passes there with or without it.

  • New check in guest_standard_streams:
    • Unbuffered stream: ten pushbacks after three reads, then reading them back followed by the rest of the file. Then three pushbacks after end-of-file, then pushbacks that rewind and fclose discard.
    • Buffered stream: forty pushbacks after the first read of a 5000-byte file. Then the bytes read back, ftello back at 1, the remaining 4999 bytes, and two more pushbacks after end-of-file.
  • Negative control: with only FileStream.hpp and StandardStreams.cpp reverted, the new check fails at its first loop of pushbacks. On main itself the getdelim fixture fails at its second ungetc, as #17423 shows.
  • guest_standard_streams passes in 3 of 3 runs. On main it fails in 3 of 3 runs.
  • The libc tests built from this head, without the tests that drive the GPU, audio, video or controllers (201): all pass, with guest_fopen_errors skipped.
  • check_conventions, git diff --check: clean. pr_overlap reports no open PR with the same functions. It could not compare 9 PRs that conflict with main on these files: #5021, #7447, #8150, #11831, #12682, #12688, #12700, #12965 and #17062. Their diffs were read by hand. None of them changes ungetc, ungetwc or the stream buffer. #7447 only adds a test that pushes back one byte on an archive stream.

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: Fixes #17423
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5214597476 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17459). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-08T19:55:09Z. --- ### What Fixes #17423: `guest_standard_streams` aborts on Windows on current `main`, because the getdelim read-error fixture from #12920 pushes back two bytes on an unbuffered stream and the second `ungetc` returns `EOF`. `ungetc` and `ungetwc` forwarded to the host `ungetc`. The UCRT pushes back by moving its read pointer back inside the stream buffer, so it has no room once the pointer sits at the start of the buffer with unread bytes in it: on an unbuffered stream (a one-byte buffer) after one pushback, and on a buffered stream after one pushback into a buffer that a read just filled. FreeBSD keeps a separate pushback buffer that grows, so it accepts these calls. On Windows, `FileStream::PushBack` now makes room before the host `ungetc` when there is none. Under the stream lock, it moves the unread bytes to the end of a buffer twice their size (at least 16 bytes more, and never smaller than the stream's buffer size), so the host `ungetc` can step back into it: - A buffered stream owns its buffer through the UCRT (`_IOBUFFER_CRT`). The new buffer replaces it, and the UCRT frees it as before. The buffer size the UCRT reads with does not change. - An unbuffered stream (`_IOBUFFER_NONE`) reads through its one-byte character buffer, which the UCRT never frees. The new buffer belongs to the `FileStream`, and the UCRT keeps reading one byte at a time into its start. The `FileStream` frees it on close, on reopen, on detaching a host standard stream and on destruction, first pointing the stream back at its character buffer if it still uses the new one. - A stream with a user buffer keeps the old behavior. `setvbuf` never hands the UCRT a user buffer, so no stream has one. Linux and macOS keep calling the host `ungetc`, which already grows its pushback room. ### Tested Windows 11 Pro 26200, MinGW-w64 GCC 15.2 (UCRT), Release, on `main` `095ff72367c`, and again merged onto `main` `59f3a64cf61`. Not run on Linux or macOS: the change is Windows only, so the new check passes there with or without it. - New check in `guest_standard_streams`: - Unbuffered stream: ten pushbacks after three reads, then reading them back followed by the rest of the file. Then three pushbacks after end-of-file, then pushbacks that `rewind` and `fclose` discard. - Buffered stream: forty pushbacks after the first read of a 5000-byte file. Then the bytes read back, `ftello` back at 1, the remaining 4999 bytes, and two more pushbacks after end-of-file. - Negative control: with only `FileStream.hpp` and `StandardStreams.cpp` reverted, the new check fails at its first loop of pushbacks. On `main` itself the getdelim fixture fails at its second `ungetc`, as #17423 shows. - `guest_standard_streams` passes in 3 of 3 runs. On `main` it fails in 3 of 3 runs. - The libc tests built from this head, without the tests that drive the GPU, audio, video or controllers (201): all pass, with `guest_fopen_errors` skipped. - `check_conventions`, `git diff --check`: clean. `pr_overlap` reports no open PR with the same functions. It could not compare 9 PRs that conflict with `main` on these files: #5021, #7447, #8150, #11831, #12682, #12688, #12700, #12965 and #17062. Their diffs were read by hand. None of them changes `ungetc`, `ungetwc` or the stream buffer. #7447 only adds a test that pushes back one byte on an archive stream. ### 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: Fixes #17423 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-11 17:17:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-11T17:17:12Z.

Review: CHANGES_REQUESTED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • 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 #1067: guest_standard_streams_closed_stdout ..........   Passed    0.01 sec
    2/5 Test #1068: guest_standard_streams_exclusive_stdout .......   Passed    0.01 sec
    3/5 Test #1069: guest_standard_streams_reopen_closed_stdout ...   Passed    0.01 sec
    4/5 Test #1070: guest_standard_streams_update_stdout ..........   Passed    0.01 sec
    5/5 Test #1066: guest_standard_streams ........................   Passed    0.25 sec
    100% tests passed, 0 tests failed out of 5
    
  • Negative control: the tests still pass with the change reverted, so they do not cover it
  • Code reading:
    • Verified core/libs/prx/libc/include/FileStream.hpp: MakePushbackRoomUnlocked ensures memory safety and geometric buffer expansion via std::max({count * 2, count + 16, bufferSize}), placing unread bytes at grown + size - count without buffer overflow.
    • Verified core/libs/prx/libc/include/FileStream.hpp: PushBack holds ::_lock_file across both MakePushbackRoomUnlocked and ::_ungetc_nolock, guaranteeing atomic inspection and mutation of UCRT stream pointers and flags without TOCTOU races.
    • Verified core/libs/prx/libc/include/FileStream.hpp: ReleasePushbackBuffer correctly restores state->base to &state->characterBuffer and zeroes state->count for unbuffered streams, preventing UCRT use-after-free or double-free across Close, Reopen, Detach, and destruction.
    • Verified core/libs/prx/libc/src/StandardStreams.cpp: ungetc_nid_postfix and ungetwc_nid_postfix maintain exact validation order, error codes (EBADF 9, EINVAL 22, EILSEQ 86), stream orientation, and read-switch transitions while delegating pushback to FileStream::PushBack.
    • Verified core/libs/tests/GuestStandardStreams.cpp: CheckRepeatedPushback covers multi-byte unbuffered pushbacks, large buffered pushbacks, post-EOF pushback behavior, feof clearing, and stream closing without resource leaks.
    • Verified AGENTS.md compliance: no comments added to code, conventional commit formatting followed, standard layout invariant preserved (std::is_standard_layout_v), and non-Windows platforms remain completely unaffected.
  • Minor: please drop the Generated with footer from the description; the AI-assisted checkbox can stay.

Requesting changes for the points above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5862650903 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17459#pullrequestreview-5862650903). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-11T17:17:12Z. **Review:** CHANGES_REQUESTED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `c5df20acbd`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **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 #1067: guest_standard_streams_closed_stdout .......... Passed 0.01 sec 2/5 Test #1068: guest_standard_streams_exclusive_stdout ....... Passed 0.01 sec 3/5 Test #1069: guest_standard_streams_reopen_closed_stdout ... Passed 0.01 sec 4/5 Test #1070: guest_standard_streams_update_stdout .......... Passed 0.01 sec 5/5 Test #1066: guest_standard_streams ........................ Passed 0.25 sec 100% tests passed, 0 tests failed out of 5 ``` - **Negative control**: the tests still pass with the change reverted, so they do not cover it - **Code reading**: - Verified core/libs/prx/libc/include/FileStream.hpp: MakePushbackRoomUnlocked ensures memory safety and geometric buffer expansion via std::max({count * 2, count + 16, bufferSize}), placing unread bytes at grown + size - count without buffer overflow. - Verified core/libs/prx/libc/include/FileStream.hpp: PushBack holds ::_lock_file across both MakePushbackRoomUnlocked and ::_ungetc_nolock, guaranteeing atomic inspection and mutation of UCRT stream pointers and flags without TOCTOU races. - Verified core/libs/prx/libc/include/FileStream.hpp: ReleasePushbackBuffer correctly restores state->base to &state->characterBuffer and zeroes state->count for unbuffered streams, preventing UCRT use-after-free or double-free across Close, Reopen, Detach, and destruction. - Verified core/libs/prx/libc/src/StandardStreams.cpp: ungetc_nid_postfix and ungetwc_nid_postfix maintain exact validation order, error codes (EBADF 9, EINVAL 22, EILSEQ 86), stream orientation, and read-switch transitions while delegating pushback to FileStream::PushBack. - Verified core/libs/tests/GuestStandardStreams.cpp: CheckRepeatedPushback covers multi-byte unbuffered pushbacks, large buffered pushbacks, post-EOF pushback behavior, feof clearing, and stream closing without resource leaks. - Verified AGENTS.md compliance: no comments added to code, conventional commit formatting followed, standard layout invariant preserved (std::is_standard_layout_v<FileStream>), and non-Windows platforms remain completely unaffected. - Minor: please drop the `Generated with` footer from the description; the AI-assisted checkbox can stay. Requesting changes for the points above.
PrivacyOnion commented 2026-11-12 22:00:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T22:00:12Z.

Review: CHANGES_REQUESTED


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

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • 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 #1182: guest_standard_streams_closed_stdout ..........   Passed    0.01 sec
    2/5 Test #1183: guest_standard_streams_exclusive_stdout .......   Passed    0.01 sec
    3/5 Test #1184: guest_standard_streams_reopen_closed_stdout ...   Passed    0.01 sec
    4/5 Test #1185: guest_standard_streams_update_stdout ..........   Passed    0.01 sec
    5/5 Test #1181: guest_standard_streams ........................   Passed    0.17 sec
    100% tests passed, 0 tests failed out of 5
    
  • Negative control: the tests still pass with the change reverted, so they do not cover it
  • Code reading:
    • Verified in core/libs/prx/libc/include/FileStream.hpp (MakePushbackRoomUnlocked) that buffer growth calculation std::max({count * 2, count + 16, bufferSize}) guarantees at least 16 bytes of headroom and that std::memcpy into grown + size - count safely avoids buffer overruns.
    • Verified in core/libs/prx/libc/include/FileStream.hpp (MakePushbackRoomUnlocked, ReleasePushbackBuffer) that stream buffer ownership properly differentiates CRT-managed buffers (UcrtCrtBuffer 0x0040 freed on replacement or UCRT fclose) and FileStream-managed unbuffered streams (UcrtNoBuffer 0x0400 safely reclaimed in ReleasePushbackBuffer on Close, Reopen, Adopt, Detach, and ~FileStream).
    • Verified in core/libs/prx/libc/include/FileStream.hpp (PushBack) that ::_lock_file(native) and ::_unlock_file(native) guard both MakePushbackRoomUnlocked and ::_ungetc_nolock, preventing concurrency races and TOCTOU inconsistencies during pointer adjustment.
    • Verified in core/libs/prx/libc/src/StandardStreams.cpp (ungetc_nid_postfix, ungetwc_nid_postfix) that forwarding to FileStream::PushBack preserves stream orientation, status synchronization via SyncStatus(), wide surrogate bookkeeping in NotePushback(), and POSIX/Orbis errno conventions (EBADF, EILSEQ).
    • Verified in core/libs/tests/GuestStandardStreams.cpp (CheckRepeatedPushback) that tests thoroughly cover repeated pushbacks beyond initial buffer capacities on both unbuffered and fully buffered files, verifying correct byte order recovery, ftello tracking, EOF reset, and rewind cleanup.
    • Verified strict compliance with AGENTS.md rules across all modified files, confirming zero code comments (except permitted #endif guards), no stubbed exports or special-cased values, and adherence to Conventional Commits.
  • Note: the AI-assisted checkbox can stay.

Requesting changes for the points above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5882676253 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17459#pullrequestreview-5882676253). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T22:00:12Z. **Review:** CHANGES_REQUESTED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `d8f7d24ea7`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **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 #1182: guest_standard_streams_closed_stdout .......... Passed 0.01 sec 2/5 Test #1183: guest_standard_streams_exclusive_stdout ....... Passed 0.01 sec 3/5 Test #1184: guest_standard_streams_reopen_closed_stdout ... Passed 0.01 sec 4/5 Test #1185: guest_standard_streams_update_stdout .......... Passed 0.01 sec 5/5 Test #1181: guest_standard_streams ........................ Passed 0.17 sec 100% tests passed, 0 tests failed out of 5 ``` - **Negative control**: the tests still pass with the change reverted, so they do not cover it - **Code reading**: - Verified in core/libs/prx/libc/include/FileStream.hpp (MakePushbackRoomUnlocked) that buffer growth calculation std::max({count * 2, count + 16, bufferSize}) guarantees at least 16 bytes of headroom and that std::memcpy into grown + size - count safely avoids buffer overruns. - Verified in core/libs/prx/libc/include/FileStream.hpp (MakePushbackRoomUnlocked, ReleasePushbackBuffer) that stream buffer ownership properly differentiates CRT-managed buffers (UcrtCrtBuffer 0x0040 freed on replacement or UCRT fclose) and FileStream-managed unbuffered streams (UcrtNoBuffer 0x0400 safely reclaimed in ReleasePushbackBuffer on Close, Reopen, Adopt, Detach, and ~FileStream). - Verified in core/libs/prx/libc/include/FileStream.hpp (PushBack) that ::_lock_file(native) and ::_unlock_file(native) guard both MakePushbackRoomUnlocked and ::_ungetc_nolock, preventing concurrency races and TOCTOU inconsistencies during pointer adjustment. - Verified in core/libs/prx/libc/src/StandardStreams.cpp (ungetc_nid_postfix, ungetwc_nid_postfix) that forwarding to FileStream::PushBack preserves stream orientation, status synchronization via SyncStatus(), wide surrogate bookkeeping in NotePushback(), and POSIX/Orbis errno conventions (EBADF, EILSEQ). - Verified in core/libs/tests/GuestStandardStreams.cpp (CheckRepeatedPushback) that tests thoroughly cover repeated pushbacks beyond initial buffer capacities on both unbuffered and fully buffered files, verifying correct byte order recovery, ftello tracking, EOF reset, and rewind cleanup. - Verified strict compliance with AGENTS.md rules across all modified files, confirming zero code comments (except permitted #endif guards), no stubbed exports or special-cased values, and adherence to Conventional Commits. - Note: the AI-assisted checkbox can stay. Requesting changes for the points above.
PrivacyOnion commented 2026-11-16 19:54:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T19:54:51Z.

Review: APPROVED


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

  • 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 #1182: guest_standard_streams_closed_stdout ..........   Passed    0.01 sec
    2/5 Test #1183: guest_standard_streams_exclusive_stdout .......   Passed    0.01 sec
    3/5 Test #1184: guest_standard_streams_reopen_closed_stdout ...   Passed    0.01 sec
    4/5 Test #1185: guest_standard_streams_update_stdout ..........   Passed    0.01 sec
    5/5 Test #1181: guest_standard_streams ........................   Passed    0.17 sec
    100% tests passed, 0 tests failed out of 5
    
  • Negative control: with core/libs/prx/libc/include/FileStream.hpp, core/libs/prx/libc/src/StandardStreams.cpp reverted to main and the new tests kept, the suite fails:
    1/5 Test #1185: guest_standard_streams_update_stdout ..........   Passed    0.01 sec
    2/5 Test #1183: guest_standard_streams_exclusive_stdout .......   Passed    0.01 sec
    3/5 Test #1182: guest_standard_streams_closed_stdout ..........   Passed    0.01 sec
    4/5 Test #1184: guest_standard_streams_reopen_closed_stdout ...   Passed    0.01 sec
    5/5 Test #1181: guest_standard_streams ........................   Passed    0.18 sec
    100% tests passed, 0 tests failed out of 5
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified in core/libs/prx/libc/include/FileStream.hpp (MakePushbackRoomUnlocked) that buffer growth calculation std::max({count * 2, count + 16, bufferSize}) guarantees at least 16 bytes of headroom and that std::memcpy into grown + size - count safely avoids buffer overruns.
    • Verified in core/libs/prx/libc/include/FileStream.hpp (MakePushbackRoomUnlocked, ReleasePushbackBuffer) that stream buffer ownership properly differentiates CRT-managed buffers (UcrtCrtBuffer 0x0040 freed on replacement or UCRT fclose) and FileStream-managed unbuffered streams (UcrtNoBuffer 0x0400 safely reclaimed in ReleasePushbackBuffer on Close, Reopen, Adopt, Detach, and ~FileStream).
    • Verified in core/libs/prx/libc/include/FileStream.hpp (PushBack) that ::_lock_file(native) and ::_unlock_file(native) guard both MakePushbackRoomUnlocked and ::_ungetc_nolock, preventing concurrency races and TOCTOU inconsistencies during pointer adjustment.
    • Verified in core/libs/prx/libc/src/StandardStreams.cpp (ungetc_nid_postfix, ungetwc_nid_postfix) that forwarding to FileStream::PushBack preserves stream orientation, status synchronization via SyncStatus(), wide surrogate bookkeeping in NotePushback(), and POSIX/Orbis errno conventions (EBADF, EILSEQ).
    • Verified in core/libs/tests/GuestStandardStreams.cpp (CheckRepeatedPushback) that tests thoroughly cover repeated pushbacks beyond initial buffer capacities on both unbuffered and fully buffered files, verifying correct byte order recovery, ftello tracking, EOF reset, and rewind cleanup.
    • Verified strict compliance with AGENTS.md rules across all modified files, confirming zero code comments (except permitted #endif guards), no stubbed exports or special-cased values, and adherence to Conventional Commits.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5926540763 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17459#pullrequestreview-5926540763). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T19:54:51Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `d8f7d24ea7`: - **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 #1182: guest_standard_streams_closed_stdout .......... Passed 0.01 sec 2/5 Test #1183: guest_standard_streams_exclusive_stdout ....... Passed 0.01 sec 3/5 Test #1184: guest_standard_streams_reopen_closed_stdout ... Passed 0.01 sec 4/5 Test #1185: guest_standard_streams_update_stdout .......... Passed 0.01 sec 5/5 Test #1181: guest_standard_streams ........................ Passed 0.17 sec 100% tests passed, 0 tests failed out of 5 ``` - **Negative control**: with `core/libs/prx/libc/include/FileStream.hpp`, `core/libs/prx/libc/src/StandardStreams.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/5 Test #1185: guest_standard_streams_update_stdout .......... Passed 0.01 sec 2/5 Test #1183: guest_standard_streams_exclusive_stdout ....... Passed 0.01 sec 3/5 Test #1182: guest_standard_streams_closed_stdout .......... Passed 0.01 sec 4/5 Test #1184: guest_standard_streams_reopen_closed_stdout ... Passed 0.01 sec 5/5 Test #1181: guest_standard_streams ........................ Passed 0.18 sec 100% tests passed, 0 tests failed out of 5 ``` Restoring the change makes it pass again. - **Code reading**: - Verified in core/libs/prx/libc/include/FileStream.hpp (MakePushbackRoomUnlocked) that buffer growth calculation std::max({count * 2, count + 16, bufferSize}) guarantees at least 16 bytes of headroom and that std::memcpy into grown + size - count safely avoids buffer overruns. - Verified in core/libs/prx/libc/include/FileStream.hpp (MakePushbackRoomUnlocked, ReleasePushbackBuffer) that stream buffer ownership properly differentiates CRT-managed buffers (UcrtCrtBuffer 0x0040 freed on replacement or UCRT fclose) and FileStream-managed unbuffered streams (UcrtNoBuffer 0x0400 safely reclaimed in ReleasePushbackBuffer on Close, Reopen, Adopt, Detach, and ~FileStream). - Verified in core/libs/prx/libc/include/FileStream.hpp (PushBack) that ::_lock_file(native) and ::_unlock_file(native) guard both MakePushbackRoomUnlocked and ::_ungetc_nolock, preventing concurrency races and TOCTOU inconsistencies during pointer adjustment. - Verified in core/libs/prx/libc/src/StandardStreams.cpp (ungetc_nid_postfix, ungetwc_nid_postfix) that forwarding to FileStream::PushBack preserves stream orientation, status synchronization via SyncStatus(), wide surrogate bookkeeping in NotePushback(), and POSIX/Orbis errno conventions (EBADF, EILSEQ). - Verified in core/libs/tests/GuestStandardStreams.cpp (CheckRepeatedPushback) that tests thoroughly cover repeated pushbacks beyond initial buffer capacities on both unbuffered and fully buffered files, verifying correct byte order recovery, ftello tracking, EOF reset, and rewind cleanup. - Verified strict compliance with AGENTS.md rules across all modified files, confirming zero code comments (except permitted #endif guards), no stubbed exports or special-cased values, and adherence to Conventional Commits. Looks Good To Me
MarcZX10 commented 2026-11-20 11:54:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T11:54:08Z.

Review: COMMENTED


Reviewed — no blocking findings

Nothing blocks this change; the points below are optional.

What I ran

Reviewed head fbdd3b3ed144e96d0e61e4a26f70ef4f782eb33a using repo_read.py. No commands or tests were run in this review. The description reports Windows 11/UCRT test runs; those results are author-reported.

Results

Check Result Note
Source review passed All three changed files read
Tests in this review not run Reader-only access
Author-reported tests passed Windows runs reported in description

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not independently run tests or verify the reported Windows environment and results. No base merge check was run. Read description.md, commits.md, manifest.json, diff.patch, and all three changed files: core/libs/prx/libc/include/FileStream.hpp, core/libs/prx/libc/src/StandardStreams.cpp, and core/libs/tests/GuestStandardStreams.cpp. No changed files were excluded.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5992240864 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17459#pullrequestreview-5992240864). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T11:54:08Z. **Review:** COMMENTED --- # Reviewed — no blocking findings Nothing blocks this change; the points below are optional. ## What I ran Reviewed head `fbdd3b3ed144e96d0e61e4a26f70ef4f782eb33a` using `repo_read.py`. No commands or tests were run in this review. The description reports Windows 11/UCRT test runs; those results are author-reported. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | All three changed files read | | Tests in this review | not run | Reader-only access | | Author-reported tests | passed | Windows runs reported in description | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not independently run tests or verify the reported Windows environment and results. No base merge check was run. Read `description.md`, `commits.md`, `manifest.json`, `diff.patch`, and all three changed files: `core/libs/prx/libc/include/FileStream.hpp`, `core/libs/prx/libc/src/StandardStreams.cpp`, and `core/libs/tests/GuestStandardStreams.cpp`. No changed files were excluded. ## To get this to a good state Nothing is required.
PrivacyOnion commented 2026-11-20 12:19:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-20T12:19:04Z.

Review: APPROVED


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

  • 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 #1372: guest_standard_streams_exclusive_stdout .......   Passed    0.03 sec
    2/5 Test #1373: guest_standard_streams_reopen_closed_stdout ...   Passed    0.02 sec
    3/5 Test #1374: guest_standard_streams_update_stdout ..........   Passed    0.01 sec
    4/5 Test #1371: guest_standard_streams_closed_stdout ..........   Passed    0.01 sec
    5/5 Test #1370: guest_standard_streams ........................   Passed    0.20 sec
    100% tests passed, 0 tests failed out of 5
    
  • Negative control: with core/libs/prx/libc/include/FileStream.hpp, core/libs/prx/libc/src/StandardStreams.cpp reverted to main and the new tests kept, the suite fails:
    1/5 Test #1372: guest_standard_streams_exclusive_stdout .......   Passed    0.02 sec
    2/5 Test #1373: guest_standard_streams_reopen_closed_stdout ...   Passed    0.01 sec
    3/5 Test #1374: guest_standard_streams_update_stdout ..........   Passed    0.01 sec
    4/5 Test #1371: guest_standard_streams_closed_stdout ..........   Passed    0.01 sec
    5/5 Test #1370: guest_standard_streams ........................   Passed    0.19 sec
    100% tests passed, 0 tests failed out of 5
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified in core/libs/prx/libc/include/FileStream.hpp (MakePushbackRoomUnlocked, ReleasePushbackBuffer, PushBack) that buffer reallocation correctly computes size with std::max({count * 2, count + 16, ...}), memcpy aligns data to the end of the buffer, and UcrtStreamState pointers (base, pointer) are updated atomically under file lock.
    • Verified in core/libs/prx/libc/include/FileStream.hpp that resource lifecycles (destructor ~FileStream, Detach, Adopt, Reopen, and Close) properly acquire native stream locks via _lock_file before releasing or resetting pushbackBuffer, preventing memory leaks or dangling pointers in the UCRT.
    • Verified in core/libs/prx/libc/src/StandardStreams.cpp that ungetc_nid_postfix and ungetwc_nid_postfix route through FileStream::PushBack, preserving POSIX/Orbis errno conventions (EBADF for unopen stream, EILSEQ for invalid wide character), EOF handling, and stream status synchronization via SyncStatus.
    • Verified in core/libs/prx/libc/src/StandardStreams.cpp that ungetwc correctly unwinds multi-byte UTF-8 sequences and surrogate pairs (TakeLowSurrogate/SetLowSurrogate) when PushBack fails mid-character.
    • Verified in core/libs/tests/GuestStandardStreams.cpp (CheckRepeatedPushback) that tests thoroughly cover repeated pushbacks beyond initial buffer capacities on both unbuffered and fully buffered files, verifying correct byte order recovery, ftello tracking, EOF reset, and rewind cleanup.
    • Verified strict compliance with AGENTS.md rules across all modified files, confirming zero code comments (except permitted #endif guards), no stubbed exports or special-cased values, and adherence to Conventional Commits.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5992537893 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17459#pullrequestreview-5992537893). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-20T12:19:04Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `ea26d492de`: - **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 #1372: guest_standard_streams_exclusive_stdout ....... Passed 0.03 sec 2/5 Test #1373: guest_standard_streams_reopen_closed_stdout ... Passed 0.02 sec 3/5 Test #1374: guest_standard_streams_update_stdout .......... Passed 0.01 sec 4/5 Test #1371: guest_standard_streams_closed_stdout .......... Passed 0.01 sec 5/5 Test #1370: guest_standard_streams ........................ Passed 0.20 sec 100% tests passed, 0 tests failed out of 5 ``` - **Negative control**: with `core/libs/prx/libc/include/FileStream.hpp`, `core/libs/prx/libc/src/StandardStreams.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/5 Test #1372: guest_standard_streams_exclusive_stdout ....... Passed 0.02 sec 2/5 Test #1373: guest_standard_streams_reopen_closed_stdout ... Passed 0.01 sec 3/5 Test #1374: guest_standard_streams_update_stdout .......... Passed 0.01 sec 4/5 Test #1371: guest_standard_streams_closed_stdout .......... Passed 0.01 sec 5/5 Test #1370: guest_standard_streams ........................ Passed 0.19 sec 100% tests passed, 0 tests failed out of 5 ``` Restoring the change makes it pass again. - **Code reading**: - Verified in core/libs/prx/libc/include/FileStream.hpp (MakePushbackRoomUnlocked, ReleasePushbackBuffer, PushBack) that buffer reallocation correctly computes size with std::max({count * 2, count + 16, ...}), memcpy aligns data to the end of the buffer, and UcrtStreamState pointers (base, pointer) are updated atomically under file lock. - Verified in core/libs/prx/libc/include/FileStream.hpp that resource lifecycles (destructor ~FileStream, Detach, Adopt, Reopen, and Close) properly acquire native stream locks via _lock_file before releasing or resetting pushbackBuffer, preventing memory leaks or dangling pointers in the UCRT. - Verified in core/libs/prx/libc/src/StandardStreams.cpp that ungetc_nid_postfix and ungetwc_nid_postfix route through FileStream::PushBack, preserving POSIX/Orbis errno conventions (EBADF for unopen stream, EILSEQ for invalid wide character), EOF handling, and stream status synchronization via SyncStatus. - Verified in core/libs/prx/libc/src/StandardStreams.cpp that ungetwc correctly unwinds multi-byte UTF-8 sequences and surrogate pairs (TakeLowSurrogate/SetLowSurrogate) when PushBack fails mid-character. - Verified in core/libs/tests/GuestStandardStreams.cpp (CheckRepeatedPushback) that tests thoroughly cover repeated pushbacks beyond initial buffer capacities on both unbuffered and fully buffered files, verifying correct byte order recovery, ftello tracking, EOF reset, and rewind cleanup. - Verified strict compliance with AGENTS.md rules across all modified files, confirming zero code comments (except permitted #endif guards), no stubbed exports or special-cased values, and adherence to Conventional Commits. Looks Good To Me
MarcZX10 commented 2026-11-20 12:43:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T12:43:12Z.

Review: COMMENTED


Reviewed — comments

Nothing blocks this change, but the UCRT buffer ownership assumptions need confirmation before approval.

What I ran

Reviewed head ea26d492deadbca664e4ef3d6a1b2315d2280c3f. I inspected the prepared diff, all three changed files, and targeted stream setup and lifecycle code through the pinned reader, repo_read.py. No commands or tests were run.

Results

Check Result Note
Diff and changed code passed Three changed files inspected
Tests not run Author-reported results were not independently run
UCRT behavior not verified Windows runtime was not available

Findings

Blocking

None verified.

Non-blocking

  • Please verify the UCRT layout and buffer ownership assumptions against the supported Windows toolchain. MakePushbackRoomUnlocked identifies owned buffers by flag values and UcrtStreamState field layout, then frees or replaces the buffer (core/libs/prx/libc/include/FileStream.hpp:129, core/libs/prx/libc/include/FileStream.hpp:141). The description reports Windows testing, but I did not independently confirm these assumptions against the runtime.

Not verified

  • No tests or builds were run. The Windows, Linux, and macOS behavior and the author-reported test runs were not independently confirmed.
  • Read: core/libs/prx/libc/include/FileStream.hpp, core/libs/prx/libc/src/StandardStreams.cpp, and core/libs/tests/GuestStandardStreams.cpp (full text included in the review bundle); targeted setup and lifecycle ranges from core/libs/prx/libc/src/RuntimeSupport.cpp, core/libs/prx/libc/src/FileAndHeap.cpp, and core/libs/prx/libc/src/StandardStreams.cpp.
  • Excluded: no changed files were excluded according to manifest.json; the optional review/files.json document was not available through the reader.

To get this to a good state

Nothing is required. Optional: confirm the UCRT layout and ownership behavior for the supported Windows toolchain.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5992832999 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17459#pullrequestreview-5992832999). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T12:43:12Z. **Review:** COMMENTED --- # Reviewed — comments Nothing blocks this change, but the UCRT buffer ownership assumptions need confirmation before approval. ## What I ran Reviewed head `ea26d492deadbca664e4ef3d6a1b2315d2280c3f`. I inspected the prepared diff, all three changed files, and targeted stream setup and lifecycle code through the pinned reader, `repo_read.py`. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and changed code | passed | Three changed files inspected | | Tests | not run | Author-reported results were not independently run | | UCRT behavior | not verified | Windows runtime was not available | ## Findings ### Blocking None verified. ### Non-blocking - Please verify the UCRT layout and buffer ownership assumptions against the supported Windows toolchain. `MakePushbackRoomUnlocked` identifies owned buffers by flag values and `UcrtStreamState` field layout, then frees or replaces the buffer (`core/libs/prx/libc/include/FileStream.hpp:129`, `core/libs/prx/libc/include/FileStream.hpp:141`). The description reports Windows testing, but I did not independently confirm these assumptions against the runtime. ## Not verified - No tests or builds were run. The Windows, Linux, and macOS behavior and the author-reported test runs were not independently confirmed. - Read: `core/libs/prx/libc/include/FileStream.hpp`, `core/libs/prx/libc/src/StandardStreams.cpp`, and `core/libs/tests/GuestStandardStreams.cpp` (full text included in the review bundle); targeted setup and lifecycle ranges from `core/libs/prx/libc/src/RuntimeSupport.cpp`, `core/libs/prx/libc/src/FileAndHeap.cpp`, and `core/libs/prx/libc/src/StandardStreams.cpp`. - Excluded: no changed files were excluded according to `manifest.json`; the optional `review/files.json` document was not available through the reader. ## To get this to a good state Nothing is required. Optional: confirm the UCRT layout and ownership behavior for the supported Windows toolchain.
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!17459
No description provided.