fix(libc): grow the pushback room of Windows streams for repeated ungetc #17459
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/17459"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-08T19:55:09Z.
What
Fixes #17423:
guest_standard_streamsaborts on Windows on currentmain, because the getdelim read-error fixture from #12920 pushes back two bytes on an unbuffered stream and the secondungetcreturnsEOF.ungetcandungetwcforwarded to the hostungetc. 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::PushBacknow makes room before the hostungetcwhen 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 hostungetccan step back into it:_IOBUFFER_CRT). The new buffer replaces it, and the UCRT frees it as before. The buffer size the UCRT reads with does not change._IOBUFFER_NONE) reads through its one-byte character buffer, which the UCRT never frees. The new buffer belongs to theFileStream, and the UCRT keeps reading one byte at a time into its start. TheFileStreamfrees 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.setvbufnever 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
main095ff72367c, and again merged ontomain59f3a64cf61. Not run on Linux or macOS: the change is Windows only, so the new check passes there with or without it.guest_standard_streams:rewindandfclosediscard.ftelloback at 1, the remaining 4999 bytes, and two more pushbacks after end-of-file.FileStream.hppandStandardStreams.cppreverted, the new check fails at its first loop of pushbacks. Onmainitself the getdelim fixture fails at its secondungetc, as #17423 shows.guest_standard_streamspasses in 3 of 3 runs. Onmainit fails in 3 of 3 runs.guest_fopen_errorsskipped.check_conventions,git diff --check: clean.pr_overlapreports no open PR with the same functions. It could not compare 9 PRs that conflict withmainon these files: #5021, #7447, #8150, #11831, #12682, #12688, #12700, #12965 and #17062. Their diffs were read by hand. None of them changesungetc,ungetwcor the stream buffer. #7447 only adds a test that pushes back one byte on an archive stream.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)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:tools/check_conventions.pypassed;git diff --checkclean.guest_standard_stream_testsbuilt cleanly.guest_standard_streams,guest_standard_streams_closed_stdout,guest_standard_streams_exclusive_stdout,guest_standard_streams_reopen_closed_stdout,guest_standard_streams_update_stdout):Generated withfooter from the description; the AI-assisted checkbox can stay.Requesting changes for the points above.
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:tools/check_conventions.pypassed;git diff --checkclean.guest_standard_stream_testsbuilt cleanly.guest_standard_streams,guest_standard_streams_closed_stdout,guest_standard_streams_exclusive_stdout,guest_standard_streams_reopen_closed_stdout,guest_standard_streams_update_stdout):Requesting changes for the points above.
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:tools/check_conventions.pypassed.guest_standard_stream_testsbuilt cleanly.guest_standard_streams,guest_standard_streams_closed_stdout,guest_standard_streams_exclusive_stdout,guest_standard_streams_reopen_closed_stdout,guest_standard_streams_update_stdout):core/libs/prx/libc/include/FileStream.hpp,core/libs/prx/libc/src/StandardStreams.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
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
fbdd3b3ed144e96d0e61e4a26f70ef4f782eb33ausingrepo_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
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, andcore/libs/tests/GuestStandardStreams.cpp. No changed files were excluded.To get this to a good state
Nothing is required.
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:tools/check_conventions.pypassed.guest_standard_stream_testsbuilt cleanly.guest_standard_streams,guest_standard_streams_closed_stdout,guest_standard_streams_exclusive_stdout,guest_standard_streams_reopen_closed_stdout,guest_standard_streams_update_stdout):core/libs/prx/libc/include/FileStream.hpp,core/libs/prx/libc/src/StandardStreams.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
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
Findings
Blocking
None verified.
Non-blocking
MakePushbackRoomUnlockedidentifies owned buffers by flag values andUcrtStreamStatefield 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
core/libs/prx/libc/include/FileStream.hpp,core/libs/prx/libc/src/StandardStreams.cpp, andcore/libs/tests/GuestStandardStreams.cpp(full text included in the review bundle); targeted setup and lifecycle ranges fromcore/libs/prx/libc/src/RuntimeSupport.cpp,core/libs/prx/libc/src/FileAndHeap.cpp, andcore/libs/prx/libc/src/StandardStreams.cpp.manifest.json; the optionalreview/files.jsondocument 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.