libc: a failed fseek on Windows clears EOF and ungetc pushback, and gives errno 29 on pipes, where FreeBSD fails with EINVAL first; fix branch ready #5251

Closed
opened 2026-10-13 15:36:05 +00:00 by husseinkad · 2 comments
husseinkad commented 2026-10-13 15:36:05 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-13T15:36:05Z.


A failed fseeko (and so fseek) on Windows clears the stream's end-of-file flag and drops any ungetc pushback before it fails, and on a pipe it sets errno to 29 (the host's ESPIPE) where FreeBSD gives EINVAL (22) for a negative target. FreeBSD 11.4 fseek.c rejects a negative SEEK_SET or SEEK_CUR target with EINVAL before touching the stream, so the EOF state and the pushed-back character survive a failed seek. A guest that retries after a failed seek, or reads the pushed-back byte afterwards, loses data here.

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libc-failed-seek-state (one commit on a recent main: libc/src/FileAndHeap.cpp, tests/GuestStandardStreams.cpp with a new CheckFailedSeekKeepsState, and one TechnicalDebt.md line). fseeko now pre-checks the SEEK_SET and SEEK_CUR targets (using the position after pushback) and returns EINVAL, or EOVERFLOW when a SEEK_CUR sum overflows, without touching the stream.

Checks:

  • The new test fails on main (abort) and passes with the change; guest_standard_streams and file_streams pass.
  • About 2.6 million lockstep operations over 20k seeds in each of two mixes (read and EOF focused, seek focused) against a line-for-line port of the FreeBSD 11.4 refill/fgetc/fgets/fread/fseek/ftell/ungetc/clearerr/rewind code: a failed negative SEEK_SET or SEEK_CUR now keeps EOF and pushback, the SEEK_CUR after ungetc reports the right ftell, and on pipes the errno is 22 where main gave 29. No real regression: the other differences in the traces are existing host C runtime bugs that main's buffer flush happened to hide.
  • Two independent reviews. check_conventions.py is clean and pr_overlap.py finds no overlap; #4837 (closed) covered ESPIPE on pipes, not this. The branch merges cleanly alone; with the separate fix/libc-eof-sticky branch it conflicts only in two appended places (the test main and the debt file).

Not fixed here (listed in TechnicalDebt.md): a SEEK_END target before the start of the file still clears EOF and pushback in the host before failing (FreeBSD keeps both); fseeko(MAX - 1, SEEK_END) returns 22 where FreeBSD returns 84; reading after a seek to a huge offset sets ferror with errno 22 instead of EOF; SEEK_CUR after an ungetc at offset 0 succeeds where FreeBSD gives ESPIPE; a second ungetc fails and ungetc then rewind returns the pushed byte (host C runtime limits). Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5833199090 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5251). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-13T15:36:05Z. --- A failed `fseeko` (and so `fseek`) on Windows clears the stream's end-of-file flag and drops any `ungetc` pushback before it fails, and on a pipe it sets `errno` to 29 (the host's `ESPIPE`) where FreeBSD gives `EINVAL` (22) for a negative target. FreeBSD 11.4 `fseek.c` rejects a negative `SEEK_SET` or `SEEK_CUR` target with `EINVAL` before touching the stream, so the EOF state and the pushed-back character survive a failed seek. A guest that retries after a failed seek, or reads the pushed-back byte afterwards, loses data here. Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libc-failed-seek-state (one commit on a recent main: `libc/src/FileAndHeap.cpp`, `tests/GuestStandardStreams.cpp` with a new `CheckFailedSeekKeepsState`, and one `TechnicalDebt.md` line). `fseeko` now pre-checks the `SEEK_SET` and `SEEK_CUR` targets (using the position after pushback) and returns `EINVAL`, or `EOVERFLOW` when a `SEEK_CUR` sum overflows, without touching the stream. Checks: - The new test fails on main (abort) and passes with the change; `guest_standard_streams` and `file_streams` pass. - About 2.6 million lockstep operations over 20k seeds in each of two mixes (read and EOF focused, seek focused) against a line-for-line port of the FreeBSD 11.4 `refill`/`fgetc`/`fgets`/`fread`/`fseek`/`ftell`/`ungetc`/`clearerr`/`rewind` code: a failed negative `SEEK_SET` or `SEEK_CUR` now keeps EOF and pushback, the `SEEK_CUR` after `ungetc` reports the right `ftell`, and on pipes the errno is 22 where main gave 29. No real regression: the other differences in the traces are existing host C runtime bugs that main's buffer flush happened to hide. - Two independent reviews. `check_conventions.py` is clean and `pr_overlap.py` finds no overlap; #4837 (closed) covered `ESPIPE` on pipes, not this. The branch merges cleanly alone; with the separate `fix/libc-eof-sticky` branch it conflicts only in two appended places (the test `main` and the debt file). Not fixed here (listed in `TechnicalDebt.md`): a `SEEK_END` target before the start of the file still clears EOF and pushback in the host before failing (FreeBSD keeps both); `fseeko(MAX - 1, SEEK_END)` returns 22 where FreeBSD returns 84; reading after a seek to a huge offset sets `ferror` with errno 22 instead of EOF; `SEEK_CUR` after an `ungetc` at offset 0 succeeds where FreeBSD gives `ESPIPE`; a second `ungetc` fails and `ungetc` then `rewind` returns the pushed byte (host C runtime limits). Only Windows was built and run. AI-assisted: yes
MarcZX10 commented 2026-10-13 16:26:43 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T16:26:43Z.


Implemented and opened as #5282: https://github.com/boykopovar/AnyPS5/pull/5282. Linux guest_standard_streams and file_streams tests pass (2/2), including negative absolute seek on a pipe; conventions checks pass. I did not run the Windows build locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6150195886 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5251#issuecomment-6150195886). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-13T16:26:43Z. --- Implemented and opened as #5282: https://github.com/boykopovar/AnyPS5/pull/5282. Linux `guest_standard_streams` and `file_streams` tests pass (2/2), including negative absolute seek on a pipe; conventions checks pass. I did not run the Windows build locally.
oneandonlydean commented 2026-10-14 06:54:32 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:54:32Z.


Closing: no title is known to reach this, and functions are only changed when a title needs them. #5282 is closed for the same reason. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162713956 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5251#issuecomment-6162713956). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T06:54:32Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. #5282 is closed for the same reason. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.
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#5251
No description provided.