libc: fseek and ftell succeed on a pipe and lose buffered data on Windows, where FreeBSD fails with ESPIPE; fix branch ready #4837
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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 DavidSamir on 2026-10-12T23:21:05Z.
On Windows,
fseek,fseeko,ftell,ftello,fgetpos,fsetposandrewindsucceed on a stream over a pipe, and the seeks lose bytes the stream had already read into its buffer. FreeBSD and the Linux build fail withESPIPEand leave the stream as it was. A program that asksfseekwhether its input can be rewound, or measures it withfseek(SEEK_END)andftell, treats a pipe as a file on Windows and loses part of what was in it.This is the stream side of #4670 (
lseekon a pipe, carried in #4672), which listed it as not changed: the streams do not go throughlseek.Cause.
fseeko_nid_postfixandftello_nid_postfixcall the C runtime's_fseeki64and_ftelli64. Windows accepts a file position on a pipe handle, so the C runtime discards its read buffer and reports success.guest_standard_streamsalready checksrewindon a pipe, but only on Linux (#ifndef _WIN32).Observed on
main0428328b7, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. A pipe frompipe()holdshello world; both ends are wrapped withfdopen:ftell(reader)fseek(reader, 0, SEEK_SET),SEEK_CUR,SEEK_ENDfgetpos(reader, &position)fread5, thenftell(reader)rewind(reader),fgetc(reader)lfwrite("abc")to the writer, close it, read the restdabc, 4 bytesworldabc, 8 bytesThe last two rows are the loss: of
world, which was still to be read afterhello, onlylanddarrive.I have not found a title that stops on this. It came out of running one probe of the stream functions on the Windows build and on the Linux build and comparing the two outputs.
Fix, ready to cherry-pick.
DavidSamir:fix/libc-seek-pipe-stream-windows(76cca64e0, one commit onmain0428328b7, three files, +38/-7). I can't open it as a PR while #4532 is open (one open PR without a merged one).Windows only.
fseekoandftelloreturn -1 withESPIPEwhen the stream's descriptor is a pipe, before the C runtime is called, so the buffer is not touched;fseek,ftell,fgetpos,fsetposandrewindgo through those two. An invalidwhencestill fails withEINVALfirst, as on FreeBSD. Whether a stream is on a pipe is asked once (GetFileType) and kept in theFileStream, and asked again afterfreopen, so a seek on a file costs what it costs today.Not changed:
dup2after its first seek orftellkeeps the answer it had, and seeks as today. Asking on every call would cover it at about 0.1 µs per call; say if you prefer that.guest_standard_streams: the pipe check inCheckRewindnow runs on Windows too and is extended. Withpqin the pipe andpread,fseekowith each of the three origins andftellomust fail with errno 29 without setting the error flag, the nextfgetcmust returnq, andrewindat the end must leave errno 29 with the end-of-file and error flags clear.Checked on Windows (76cca64e0 on
main0428328b7, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):FileAndHeap.cppandFileStream.hppas onmainand the test kept,guest_standard_streamsfails (exit 0xc0000409, the test'sRequireaborting). With the fix it passes 5/5 (--repeat until-fail:5), andfile_streams,file_position,guest_fiopen,guest_filesystem,guest_file_vector,guest_dupandguest_pollpass 3/3.main's libraries and the branch's: the 17 pipe lines change to what the Linux build prints; the lines for a file (ftell,fseekwith each origin, a negative offset,fgetpos,rewind) are identical in both. Against the Linux build the branch differs in two lines, thedup2case above.fopenin 43 modes, seeking,ungetc,fgets, buffering,freopen,fdopen), run with both sets of libraries: two lines differ,ftellandfseekon a pipe. A probe of 122 lines over the descriptor functions is identical in both.ftell, two millionfseekand one millionfseekplusfgetc, nine passes each in three alternating runs:ftell309 to 350 ns withmain's libraries and 326 to 357 ns with the branch's;fseek181 to 352 ns and 182 to 336 ns; the pair 338 to 376 ns and 341 to 383 ns. No difference I can separate from the run-to-run spread.pollin both (#3644). I do not know whether any of them seeks on a pipe.ctest --parallel 4: 1001 of 1001 pass (7 skipped, no display).tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchlists #4738 (thefdopenappend fix from #4735) for one place inFileAndHeap.cpp: both add a helper afterOpenNative, so whichever lands second needs a small rebase, which I can do.Checked on Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release):
guest_standard_streamsandfile_streamspass 3/3, so the extended check matches the existing Linux behaviour. Only those targets andlibswere built there.Not checked on a console or with a commercial title.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T23:24:45Z.
Carried in #4838, keeping your commit and authorship. I added the full Linux run (Ubuntu 24.04, GCC 13.3): all 324
guest_*tests pass in three runs, and the extended pipe check also passes withmain'sFileAndHeap.cppandFileStream.hpp, so Linux is unchanged. The Windows results in the PR are yours from this issue. I'll rebase whichever of #4738 and #4838 lands second. Thanks for the branch.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-12T23:25:40Z.
Carried as #4839: your commit cherry-picked unchanged onto current
main, authorship kept. Thanks for the detailed write-up.