fix(libc): clear the orientation when freopen opens a closed stream #23948
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/23948"
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 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 byfclose,fwide(stream, 1)(orfgetwc,fgetws,fwscanf, which orient the stream before they find it closed and fail withEBADF) sets the orientation;freopen(path, "w", stream)then succeeded, andfwide(stream, 0)on the new file still returned 1 instead of 0, andfwide(stream, -1)could no longer make it byte-oriented.The
fwidedebt entry states the rule: "a successfulfreopenclears it".FileStream::Reopenapplies it on the path for a stream that is still open (_orientation.store(0)before the hostfreopen), andCloseandDetachclear it too. The path for a closed stream returnedAdopt(open(filename, mode))directly, andAdoptresets 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
freopenof a closed stream is unchanged: it returns beforeAdoptand 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.CheckClosedStreamReopenincore/libs/tests/GuestStandardStreams.cpp, which already closes a stream and reopens it: orient the closed stream, then after the successfulfreopenrequirefwide(stream, 0) == 0, and-1after the followingfputs.main9b11f4281eandFileStream.hppunchanged,guest_standard_streamsaborts.ctest -R "^guest_standard_streams|fwide"passes 6 of 6 (guest_fwide,guest_standard_streamsand its four standard-output variants), built with theguest_standard_stream_testsandguest_fwide_teststargets.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/mainpasses. No debt change: the entry already describes the behaviour this PR makes true.python tools/pr_overlap.py --branch fix/libc-freopen-closed-stream-orientationreports #11831, which rewrites the samereturn 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 withmainonFileStream.hpp; compared by hand, none of them changes_orientationor that line.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-19T22:46:45Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
36651ef7fd: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.hppreverted 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-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
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.