fix(libc): fail fseek and ftell on a pipe with ESPIPE on Windows #4838
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4838"
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 milescoviello on 2026-10-12T23:24:36Z.
What
Fixes #4837. Carries @DavidSamir's fix branch from that issue (
DavidSamir:fix/libc-seek-pipe-stream-windows,76cca64e), keeping the commit and its authorship; it is rebased onto currentmainafter #4738 merged; both add a Windows helper afterOpenNativeinFileAndHeap.cpp, and both helpers are kept.On Windows,
fseek,fseeko,ftell,ftello,fgetpos,fsetposandrewindsucceeded on a stream over a pipe, because_fseeki64and_ftelli64accept a position on a pipe handle, and the seeks discarded bytes the stream had already read into its buffer. FreeBSD and the Linux build fail withESPIPEand leave the stream as it was. This is the stream side of #4670 (lseekon a pipe, carried in #4672), which listed it as not changed because streams do not go throughlseek. On Windows these calls now fail withESPIPEfor a stream over a pipe, before the C runtime touches its buffer. Linux is unchanged.A second commit, after @MarcZX100's review, drops the per-stream cache of that check: a stream first used on a file stayed classified as a file after its descriptor was replaced with a pipe (
dup2ontofileno(stream)), sofseekoandftellonow askGetFileTypeon every call, aslseekon a pipe does since #4670, andFileStream.hppis back as onmain.guest_standard_streamsextends its pipe check: on both ends wrapped withfdopen, the seek and position calls fail with errno 29 and the data already buffered is still read back afterwards.Tested
main'sFileAndHeap.cppandFileStream.hppthe extended check fails and with the fixguest_standard_streamspasses 5/5; a 44-line pipe probe changes only the 17 pipe lines, to what the Linux build prints, and an 884-line stream probe differs only inftell/fseekon a pipe; eleven catalog titles end the same way with both sets of libraries; fullctest1001/1001; no measurable cost on a file.mainc13dc7e6.guest_standard_streams,file_streams,file_positionandguest_filesystempass, and all 325guest_*tests pass in two runs. The extended check also passes withmain'sFileAndHeap.cppandFileStream.hpp, as expected since only the Windows path changes. I could not run Windows myself.-fsyntax-only -Wall -Wextracompile ofFileAndHeap.cppfor the Windows path; it was not run on Windows.tools/check_conventions.py --base upstream/mainpasses.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T23:33:45Z.
I checked the pipe detection against descriptor replacement.
FileStream::IsPipecaches the first query inpipe, so a stream first used on a regular file remains classified as non-pipe if its fd is later replaced with a pipe (for example,_dup2(pipeRead, _fileno(stream))). A laterfseekthen reaches the CRT and can discard buffered bytes instead of returningESPIPE. I have a local follow-up regression that exercises this replacement case and removes the cache. On Linux,guest_standard_streams,file_position, andfile_streamspassed (3/3); GCC/MinGW syntax checks with-Wall -Wextra -Werroralso passed. I could not run that new_dup2case on Windows locally, so that part still needs Windows runtime validation. This seems worth covering before landing, or as a focused follow-up.Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T23:35:41Z.
Good catch, thanks. I pushed 79269ffb0 on top of @DavidSamir's commit: the cache is gone,
fseekoandftelloaskGetFileTypeon every call (aslseekon a pipe does since #4670), andFileStream.hppis back as onmain. Linux tests pass (324/324) and a MinGW-w64-fsyntax-only -Wall -Wextracompile ofFileAndHeap.cppis clean, but I can't run it on Windows. If you'd like to add your_dup2regression, feel free to post it here and I'll include it with you as co-author, or open it as the follow-up.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T07:25:00Z.
Thanks for the offer and for removing the cached pipe classification. The
_dup2descriptor-replacement case is a useful focused regression for the merged behavior. I don't have a Windows runtime result for that scenario yet, so I won't claim that it has been covered or validated; I'll keep the caveat explicit if I follow up with the test.