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

Closed
nicolasfalesy wants to merge 1 commit from upstream-pr/4839 into main
nicolasfalesy commented 2026-10-12 23:25:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-12T23:25:35Z.


What

Carries DavidSamir:fix/libc-seek-pipe-stream-windows (76cca64e0) from #4837, which David can't open while #4532 is open. The commit is cherry-picked onto current main unchanged, authorship kept.

Closes #4837.

On Windows, fseeko and ftello (and through them fseek, ftell, fgetpos, fsetpos and rewind) now fail with ESPIPE on a stream over a pipe before the C runtime is called, as on FreeBSD and the Linux build, so the stream's read buffer is no longer discarded. Whether the stream is a pipe is asked once with GetFileType and kept in the FileStream, and asked again after freopen. Linux is unchanged. guest_standard_streams' pipe check now runs on Windows too and is extended to every seek origin and ftello. Details and the Windows measurements are in #4837.

Tested

  • Linux (Arch, GCC, Release) on main 90c683946 with this commit: guest_standard_streams and file_streams pass 5/5 (--repeat until-fail:5); tools/check_conventions.py --base upstream/main passes.
  • Windows: not re-tested by me. David's Windows 11 / WinLibs GCC 15.2 results are in #4837 (guest_standard_streams fails on main and passes 5/5 with the fix; full ctest 1001/1001).

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 under Silent stubs in their library's technical debt file
  • 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 (#4738 adds a helper at the same place in FileAndHeap.cpp; whichever lands second needs a small rebase, which I'll do here if this one is second)
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4836176113 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4839). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-12T23:25:35Z. --- ### What Carries [`DavidSamir:fix/libc-seek-pipe-stream-windows`](https://github.com/DavidSamir/AnyPS5/tree/fix/libc-seek-pipe-stream-windows) (76cca64e0) from #4837, which David can't open while #4532 is open. The commit is cherry-picked onto current `main` unchanged, authorship kept. Closes #4837. On Windows, `fseeko` and `ftello` (and through them `fseek`, `ftell`, `fgetpos`, `fsetpos` and `rewind`) now fail with `ESPIPE` on a stream over a pipe before the C runtime is called, as on FreeBSD and the Linux build, so the stream's read buffer is no longer discarded. Whether the stream is a pipe is asked once with `GetFileType` and kept in the `FileStream`, and asked again after `freopen`. Linux is unchanged. `guest_standard_streams`' pipe check now runs on Windows too and is extended to every seek origin and `ftello`. Details and the Windows measurements are in #4837. ### Tested - Linux (Arch, GCC, Release) on `main` 90c683946 with this commit: `guest_standard_streams` and `file_streams` pass 5/5 (`--repeat until-fail:5`); `tools/check_conventions.py --base upstream/main` passes. - Windows: not re-tested by me. David's Windows 11 / WinLibs GCC 15.2 results are in #4837 (`guest_standard_streams` fails on `main` and passes 5/5 with the fix; full `ctest` 1001/1001). ### Checklist - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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 (#4738 adds a helper at the same place in `FileAndHeap.cpp`; whichever lands second needs a small rebase, which I'll do here if this one is second) - [x] Unblocks: none - [x] AI-assisted: yes
milescoviello commented 2026-10-12 23:27:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T23:27:34Z.


For whoever reviews: #4838 carries the same commit from #4837 (opened a minute before this one, with a Linux run added), so only one of the two needs merging.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6134575532 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4839#issuecomment-6134575532). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T23:27:34Z. --- For whoever reviews: #4838 carries the same commit from #4837 (opened a minute before this one, with a Linux run added), so only one of the two needs merging. AI-assisted: yes
github-actions[bot] commented 2026-10-12 23:28:24 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-12T23:28:24Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4738 core/libs/prx/libc/src/FileAndHeap.cpp L89
#4838 core/libs/prx/libc/src/FileAndHeap.cpp L89, L348, L361
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6134584783 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4839#issuecomment-6134584783). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-12T23:28:24Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4738 | `core/libs/prx/libc/src/FileAndHeap.cpp` [L89](https://github.com/boykopovar/AnyPS5/blob/e55b881f0f29084f290bd414b2692da16cbf945c/core/libs/prx/libc/src/FileAndHeap.cpp#L89) | | | | #4838 | `core/libs/prx/libc/src/FileAndHeap.cpp` [L89](https://github.com/boykopovar/AnyPS5/blob/e55b881f0f29084f290bd414b2692da16cbf945c/core/libs/prx/libc/src/FileAndHeap.cpp#L89), [L348](https://github.com/boykopovar/AnyPS5/blob/e55b881f0f29084f290bd414b2692da16cbf945c/core/libs/prx/libc/src/FileAndHeap.cpp#L348), [L361](https://github.com/boykopovar/AnyPS5/blob/e55b881f0f29084f290bd414b2692da16cbf945c/core/libs/prx/libc/src/FileAndHeap.cpp#L361) | | |
MarcZX10 commented 2026-10-12 23:35:06 +00:00 (Migrated from github.com)

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


I found the same descriptor-replacement edge case here as in #4838: FileStream::IsPipe caches its first GetFileType result. If a stream is first queried while backed by a regular file and its descriptor is later replaced by a pipe (_dup2), subsequent seeks still treat it as a file and can discard buffered bytes. I posted the concrete case and local Linux/syntax-check results on #4838 as well. The Windows-only regression was not run locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6134663813 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4839#issuecomment-6134663813). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-12T23:35:06Z. --- I found the same descriptor-replacement edge case here as in #4838: `FileStream::IsPipe` caches its first `GetFileType` result. If a stream is first queried while backed by a regular file and its descriptor is later replaced by a pipe (`_dup2`), subsequent seeks still treat it as a file and can discard buffered bytes. I posted the concrete case and local Linux/syntax-check results on #4838 as well. The Windows-only regression was not run locally.
nicolasfalesy commented 2026-10-12 23:58:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nicolasfalesy on 2026-10-12T23:58:58Z.


Closing as a duplicate of #4838, which carries the same commit, was opened first and has the fuller Linux run. Thanks @milescoviello.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6134925502 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4839#issuecomment-6134925502). Originally posted by [nicolasfalesy](https://github.com/nicolasfalesy) on 2026-10-12T23:58:58Z. --- Closing as a duplicate of #4838, which carries the same commit, was opened first and has the fuller Linux run. Thanks @milescoviello.

Pull request closed

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!4839
No description provided.