fix(libc): fail fseek and ftell on a pipe with ESPIPE on Windows #4838

Merged
milescoviello merged 2 commits from upstream-pr/4838 into main 2026-10-13 04:51:37 +00:00
milescoviello commented 2026-10-12 23:24:36 +00:00 (Migrated from github.com)

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 current main after #4738 merged; both add a Windows helper after OpenNative in FileAndHeap.cpp, and both helpers are kept.

On Windows, fseek, fseeko, ftell, ftello, fgetpos, fsetpos and rewind succeeded on a stream over a pipe, because _fseeki64 and _ftelli64 accept 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 with ESPIPE and leave the stream as it was. This is the stream side of #4670 (lseek on a pipe, carried in #4672), which listed it as not changed because streams do not go through lseek. On Windows these calls now fail with ESPIPE for 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 (dup2 onto fileno(stream)), so fseeko and ftello now ask GetFileType on every call, as lseek on a pipe does since #4670, and FileStream.hpp is back as on main.

guest_standard_streams extends its pipe check: on both ends wrapped with fdopen, the seek and position calls fail with errno 29 and the data already buffered is still read back afterwards.

Tested

  • From the issue: Windows 11, WinLibs MinGW-w64 GCC 15.2.0, Release: with main's FileAndHeap.cpp and FileStream.hpp the extended check fails and with the fix guest_standard_streams passes 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 in ftell/fseek on a pipe; eleven catalog titles end the same way with both sets of libraries; full ctest 1001/1001; no measurable cost on a file.
  • Linux, added for this carry: Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on main c13dc7e6. guest_standard_streams, file_streams, file_position and guest_filesystem pass, and all 325 guest_* tests pass in two runs. The extended check also passes with main's FileAndHeap.cpp and FileStream.hpp, as expected since only the Windows path changes. I could not run Windows myself.
  • The second commit was checked on Linux (the same tests) and with a MinGW-w64 GCC 13 -fsyntax-only -Wall -Wextra compile of FileAndHeap.cpp for the Windows path; it was not run on Windows.
  • tools/check_conventions.py --base upstream/main passes.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4836169350 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4838). Originally posted by [milescoviello](https://github.com/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 current `main` after #4738 merged; both add a Windows helper after `OpenNative` in `FileAndHeap.cpp`, and both helpers are kept. On Windows, `fseek`, `fseeko`, `ftell`, `ftello`, `fgetpos`, `fsetpos` and `rewind` succeeded on a stream over a pipe, because `_fseeki64` and `_ftelli64` accept 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 with `ESPIPE` and leave the stream as it was. This is the stream side of #4670 (`lseek` on a pipe, carried in #4672), which listed it as not changed because streams do not go through `lseek`. On Windows these calls now fail with `ESPIPE` for 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 (`dup2` onto `fileno(stream)`), so `fseeko` and `ftello` now ask `GetFileType` on every call, as `lseek` on a pipe does since #4670, and `FileStream.hpp` is back as on `main`. `guest_standard_streams` extends its pipe check: on both ends wrapped with `fdopen`, the seek and position calls fail with errno 29 and the data already buffered is still read back afterwards. ### Tested - From the issue: Windows 11, WinLibs MinGW-w64 GCC 15.2.0, Release: with `main`'s `FileAndHeap.cpp` and `FileStream.hpp` the extended check fails and with the fix `guest_standard_streams` passes 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 in `ftell`/`fseek` on a pipe; eleven catalog titles end the same way with both sets of libraries; full `ctest` 1001/1001; no measurable cost on a file. - Linux, added for this carry: Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on `main` c13dc7e6. `guest_standard_streams`, `file_streams`, `file_position` and `guest_filesystem` pass, and all 325 `guest_*` tests pass in two runs. The extended check also passes with `main`'s `FileAndHeap.cpp` and `FileStream.hpp`, as expected since only the Windows path changes. I could not run Windows myself. - The second commit was checked on Linux (the same tests) and with a MinGW-w64 GCC 13 `-fsyntax-only -Wall -Wextra` compile of `FileAndHeap.cpp` for the Windows path; it was not run on Windows. - `tools/check_conventions.py --base upstream/main` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-10-12 23:33:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T23:33:45Z.


I checked the pipe detection against descriptor replacement. FileStream::IsPipe caches the first query in pipe, 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 later fseek then reaches the CRT and can discard buffered bytes instead of returning ESPIPE. I have a local follow-up regression that exercises this replacement case and removes the cache. On Linux, guest_standard_streams, file_position, and file_streams passed (3/3); GCC/MinGW syntax checks with -Wall -Wextra -Werror also passed. I could not run that new _dup2 case on Windows locally, so that part still needs Windows runtime validation. This seems worth covering before landing, or as a focused follow-up.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6134648753 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4838#issuecomment-6134648753). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T23:33:45Z. --- I checked the pipe detection against descriptor replacement. `FileStream::IsPipe` caches the first query in `pipe`, 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 later `fseek` then reaches the CRT and can discard buffered bytes instead of returning `ESPIPE`. I have a local follow-up regression that exercises this replacement case and removes the cache. On Linux, `guest_standard_streams`, `file_position`, and `file_streams` passed (3/3); GCC/MinGW syntax checks with `-Wall -Wextra -Werror` also passed. I could not run that new `_dup2` case on Windows locally, so that part still needs Windows runtime validation. This seems worth covering before landing, or as a focused follow-up.
milescoviello commented 2026-10-12 23:35:41 +00:00 (Migrated from github.com)

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, fseeko and ftello ask GetFileType on every call (as lseek on a pipe does since #4670), and FileStream.hpp is back as on main. Linux tests pass (324/324) and a MinGW-w64 -fsyntax-only -Wall -Wextra compile of FileAndHeap.cpp is clean, but I can't run it on Windows. If you'd like to add your _dup2 regression, 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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6134670120 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4838#issuecomment-6134670120). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T23:35:41Z. --- Good catch, thanks. I pushed 79269ffb0 on top of @DavidSamir's commit: the cache is gone, `fseeko` and `ftello` ask `GetFileType` on every call (as `lseek` on a pipe does since #4670), and `FileStream.hpp` is back as on `main`. Linux tests pass (324/324) and a MinGW-w64 `-fsyntax-only -Wall -Wextra` compile of `FileAndHeap.cpp` is clean, but I can't run it on Windows. If you'd like to add your `_dup2` regression, 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
MarcZX10 commented 2026-10-13 07:25:00 +00:00 (Migrated from github.com)

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 _dup2 descriptor-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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140514286 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4838#issuecomment-6140514286). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-13T07:25:00Z. --- Thanks for the offer and for removing the cached pipe classification. The `_dup2` descriptor-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.
Sign in to join this conversation.
No reviewers
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!4838
No description provided.