libc: fseek and ftell succeed on a pipe and lose buffered data on Windows, where FreeBSD fails with ESPIPE; fix branch ready #4837

Closed
opened 2026-10-12 23:21:05 +00:00 by DavidSamir · 2 comments
DavidSamir commented 2026-10-12 23:21:05 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-12T23:21:05Z.


On Windows, fseek, fseeko, ftell, ftello, fgetpos, fsetpos and rewind succeed 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 with ESPIPE and leave the stream as it was. A program that asks fseek whether its input can be rewound, or measures it with fseek(SEEK_END) and ftell, treats a pipe as a file on Windows and loses part of what was in it.

This is the stream side of #4670 (lseek on a pipe, carried in #4672), which listed it as not changed: the streams do not go through lseek.

Cause. fseeko_nid_postfix and ftello_nid_postfix call the C runtime's _fseeki64 and _ftelli64. Windows accepts a file position on a pipe handle, so the C runtime discards its read buffer and reports success. guest_standard_streams already checks rewind on a pipe, but only on Linux (#ifndef _WIN32).

Observed on main 0428328b7, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. A pipe from pipe() holds hello world; both ends are wrapped with fdopen:

call Windows FreeBSD, Linux build
ftell(reader) 0 -1, errno 29
fseek(reader, 0, SEEK_SET), SEEK_CUR, SEEK_END 0 -1, errno 29
fgetpos(reader, &position) 0, position overwritten -1, errno 29, position untouched
fread 5, then ftell(reader) -4 -1, errno 29
then rewind(reader), fgetc(reader) l the next byte, a space
fwrite("abc") to the writer, close it, read the rest dabc, 4 bytes worldabc, 8 bytes

The last two rows are the loss: of world, which was still to be read after hello, only l and d arrive.

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 on main 0428328b7, 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. fseeko and ftello return -1 with ESPIPE when the stream's descriptor is a pipe, before the C runtime is called, so the buffer is not touched; fseek, ftell, fgetpos, fsetpos and rewind go through those two. An invalid whence still fails with EINVAL first, as on FreeBSD. Whether a stream is on a pipe is asked once (GetFileType) and kept in the FileStream, and asked again after freopen, so a seek on a file costs what it costs today.

Not changed:

  • A stream whose descriptor is replaced by a pipe with dup2 after its first seek or ftell keeps 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.
  • Streams on a console or another character device.

guest_standard_streams: the pipe check in CheckRewind now runs on Windows too and is extended. With pq in the pipe and p read, fseeko with each of the three origins and ftello must fail with errno 29 without setting the error flag, the next fgetc must return q, and rewind at the end must leave errno 29 with the end-of-file and error flags clear.

Checked on Windows (76cca64e0 on main 0428328b7, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):

  • With FileAndHeap.cpp and FileStream.hpp as on main and the test kept, guest_standard_streams fails (exit 0xc0000409, the test's Require aborting). With the fix it passes 5/5 (--repeat until-fail:5), and file_streams, file_position, guest_fiopen, guest_filesystem, guest_file_vector, guest_dup and guest_poll pass 3/3.
  • The probe behind the table, 44 lines, with main's libraries and the branch's: the 17 pipe lines change to what the Linux build prints; the lines for a file (ftell, fseek with each origin, a negative offset, fgetpos, rewind) are identical in both. Against the Linux build the branch differs in two lines, the dup2 case above.
  • A probe of 884 lines over the stream functions (fopen in 43 modes, seeking, ungetc, fgets, buffering, freopen, fdopen), run with both sets of libraries: two lines differ, ftell and fseek on a pipe. A probe of 122 lines over the descriptor functions is identical in both.
  • Timing on a file, two million ftell, two million fseek and one million fseek plus fgetc, nine passes each in three alternating runs: ftell 309 to 350 ns with main's libraries and 326 to 357 ns with the branch's; fseek 181 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.
  • Titles from the homebrew catalog, 35 s each with both sets of libraries: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510), ProsperoTV (PPSA99003), PSXS5 (PPSA97510), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000) and OpenNOW (PPSA99082) end at the same place in both, with the same number of stderr lines; PSXS5 stops on poll in both (#3644). I do not know whether any of them seeks on a pipe.
  • Full ctest --parallel 4: 1001 of 1001 pass (7 skipped, no display).
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch lists #4738 (the fdopen append fix from #4735) for one place in FileAndHeap.cpp: both add a helper after OpenNative, 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_streams and file_streams pass 3/3, so the extended check matches the existing Linux behaviour. Only those targets and libs were built there.

Not checked on a console or with a commercial title.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5821827946 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4837). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-12T23:21:05Z. --- On Windows, `fseek`, `fseeko`, `ftell`, `ftello`, `fgetpos`, `fsetpos` and `rewind` succeed 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 with `ESPIPE` and leave the stream as it was. A program that asks `fseek` whether its input can be rewound, or measures it with `fseek(SEEK_END)` and `ftell`, treats a pipe as a file on Windows and loses part of what was in it. This is the stream side of #4670 (`lseek` on a pipe, carried in #4672), which listed it as not changed: the streams do not go through `lseek`. **Cause.** [`fseeko_nid_postfix`](https://github.com/boykopovar/AnyPS5/blob/0428328b7/core/libs/prx/libc/src/FileAndHeap.cpp#L345-L357) and [`ftello_nid_postfix`](https://github.com/boykopovar/AnyPS5/blob/0428328b7/core/libs/prx/libc/src/FileAndHeap.cpp#L359-L368) call the C runtime's `_fseeki64` and `_ftelli64`. Windows accepts a file position on a pipe handle, so the C runtime discards its read buffer and reports success. `guest_standard_streams` already checks `rewind` on a pipe, but only on Linux (`#ifndef _WIN32`). Observed on `main` 0428328b7, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. A pipe from `pipe()` holds `hello world`; both ends are wrapped with `fdopen`: | call | Windows | FreeBSD, Linux build | |---|---|---| | `ftell(reader)` | 0 | -1, errno 29 | | `fseek(reader, 0, SEEK_SET)`, `SEEK_CUR`, `SEEK_END` | 0 | -1, errno 29 | | `fgetpos(reader, &position)` | 0, position overwritten | -1, errno 29, position untouched | | `fread` 5, then `ftell(reader)` | -4 | -1, errno 29 | | then `rewind(reader)`, `fgetc(reader)` | `l` | the next byte, a space | | `fwrite("abc")` to the writer, close it, read the rest | `dabc`, 4 bytes | `worldabc`, 8 bytes | The last two rows are the loss: of ` world`, which was still to be read after `hello`, only `l` and `d` arrive. 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`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libc-seek-pipe-stream-windows) (76cca64e0, one commit on `main` 0428328b7, 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. `fseeko` and `ftello` return -1 with `ESPIPE` when the stream's descriptor is a pipe, before the C runtime is called, so the buffer is not touched; `fseek`, `ftell`, `fgetpos`, `fsetpos` and `rewind` go through those two. An invalid `whence` still fails with `EINVAL` first, as on FreeBSD. Whether a stream is on a pipe is asked once (`GetFileType`) and kept in the `FileStream`, and asked again after `freopen`, so a seek on a file costs what it costs today. Not changed: - A stream whose descriptor is replaced by a pipe with `dup2` after its first seek or `ftell` keeps 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. - Streams on a console or another character device. `guest_standard_streams`: the pipe check in `CheckRewind` now runs on Windows too and is extended. With `pq` in the pipe and `p` read, `fseeko` with each of the three origins and `ftello` must fail with errno 29 without setting the error flag, the next `fgetc` must return `q`, and `rewind` at the end must leave errno 29 with the end-of-file and error flags clear. **Checked on Windows** (76cca64e0 on `main` 0428328b7, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds): - With `FileAndHeap.cpp` and `FileStream.hpp` as on `main` and the test kept, `guest_standard_streams` fails (exit 0xc0000409, the test's `Require` aborting). With the fix it passes 5/5 (`--repeat until-fail:5`), and `file_streams`, `file_position`, `guest_fiopen`, `guest_filesystem`, `guest_file_vector`, `guest_dup` and `guest_poll` pass 3/3. - The probe behind the table, 44 lines, with `main`'s libraries and the branch's: the 17 pipe lines change to what the Linux build prints; the lines for a file (`ftell`, `fseek` with each origin, a negative offset, `fgetpos`, `rewind`) are identical in both. Against the Linux build the branch differs in two lines, the `dup2` case above. - A probe of 884 lines over the stream functions (`fopen` in 43 modes, seeking, `ungetc`, `fgets`, buffering, `freopen`, `fdopen`), run with both sets of libraries: two lines differ, `ftell` and `fseek` on a pipe. A probe of 122 lines over the descriptor functions is identical in both. - Timing on a file, two million `ftell`, two million `fseek` and one million `fseek` plus `fgetc`, nine passes each in three alternating runs: `ftell` 309 to 350 ns with `main`'s libraries and 326 to 357 ns with the branch's; `fseek` 181 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. - Titles from the homebrew catalog, 35 s each with both sets of libraries: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510), ProsperoTV (PPSA99003), PSXS5 (PPSA97510), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000) and OpenNOW (PPSA99082) end at the same place in both, with the same number of stderr lines; PSXS5 stops on `poll` in both (#3644). I do not know whether any of them seeks on a pipe. - Full `ctest --parallel 4`: 1001 of 1001 pass (7 skipped, no display). - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch` lists #4738 (the `fdopen` append fix from #4735) for one place in `FileAndHeap.cpp`: both add a helper after `OpenNative`, 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_streams` and `file_streams` pass 3/3, so the extended check matches the existing Linux behaviour. Only those targets and `libs` were built there. Not checked on a console or with a commercial title. AI-assisted: yes
milescoviello commented 2026-10-12 23:24:45 +00:00 (Migrated from github.com)
Author
Contributor

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 with main's FileAndHeap.cpp and FileStream.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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6134543046 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4837#issuecomment-6134543046). Originally posted by [milescoviello](https://github.com/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 with `main`'s `FileAndHeap.cpp` and `FileStream.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
nicolasfalesy commented 2026-10-12 23:25:40 +00:00 (Migrated from github.com)
Author
Contributor

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6134553782 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4837#issuecomment-6134553782). Originally posted by [nicolasfalesy](https://github.com/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.
Sign in to join this conversation.
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#4837
No description provided.