libc: reads after end-of-file return newly appended data on Windows, where FreeBSD keeps returning EOF until clearerr; fix branch ready #5279

Closed
opened 2026-10-13 16:23:41 +00:00 by husseinkad · 2 comments
husseinkad commented 2026-10-13 16:23:41 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-13T16:23:41Z.


After a stream reaches end-of-file, the Windows host C runtime reads again on every call, so fgetc, getc_unlocked, fgets, fread and fscanf return bytes that were appended after EOF without a clearerr. FreeBSD 11.4 refill.c keeps returning EOF until clearerr, a seek or an ungetc (if (fp->_flags & __SEOF) return (EOF)). A guest that reads a file to EOF and later polls it again (a log tail loop written for FreeBSD, for example) sees new data here where it would see EOF on the console, and mixed reads can disagree (fread consumes the byte, fscanf returns it).

Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libc-eof-sticky (four commits on a recent main: the change, a macOS-safe call form, and two follow-ups from review; libc/src/StandardStreams.cpp, FileAndHeap.cpp, Formatting.cpp, tests/GuestStandardStreams.cpp with a new CheckStickyEndOfFile, and a debt entry). Those functions now return EOF while the host EOF flag is set; fgets with size 1 is exempt, as in FreeBSD. fscanf follows the first directive of its format: it returns 0 for an empty, whitespace-only or unknown-conversion format and EOF for a literal, %% or any conversion that needs input, matching FreeBSD's vfscanf.

Checks:

  • CheckStickyEndOfFile fails on main (abort) and passes with the change; guest_standard_streams and file_streams pass.
  • Lockstep traces of about 2.6 million operations (20k seeds in each of two mixes) against a port of the FreeBSD 11.4 refill/fgetc/fgets/fread/fseek/ungetc/clearerr code, and 2,000 further read/EOF seeds against a semantic port with fscanf: zero EOF-related mismatches. The reported sequence (file x\ny; fread(1x7) returns 3; append p; fread(5x1) returns 0; fscanf("%c")) returns -1 as on FreeBSD. fscanf on stdin at EOF returns -1 and getchar keeps returning EOF; sscanf/vsscanf are unaffected; the format shapes '', ' ', '\n', '%y', '%%', 'x', %c, %s, %[a], %5c, %*d, ' %d' match vfscanf.c.
  • Three independent reviews. Two found problems that are fixed in the final commit (a stray NUL byte in a character literal and fscanf returning EOF for formats that read no input). check_conventions.py is clean, a byte scan of every changed file finds no NUL, and pr_overlap.py found no overlap by hand.

Not covered (listed in TechnicalDebt.md): a format starting with %n can still read data appended after EOF ("%n%d" returns 1, FreeBSD returns -1); host ungetc limits (a second ungetc fails, ungetc then rewind returns the pushed byte, ftello is off by one after ungetc at offset 0); stdin is in text mode, so ab\r\n reads back as ab\n. The first commit's patch still contains the NUL byte that the last commit removes, so squash on merge. It conflicts with the separate failed-seek branch (#5251) only in two appended places (the test main and the debt file). Only Windows was built and run.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5833888737 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5279). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-13T16:23:41Z. --- After a stream reaches end-of-file, the Windows host C runtime reads again on every call, so `fgetc`, `getc_unlocked`, `fgets`, `fread` and `fscanf` return bytes that were appended after EOF without a `clearerr`. FreeBSD 11.4 `refill.c` keeps returning EOF until `clearerr`, a seek or an `ungetc` (`if (fp->_flags & __SEOF) return (EOF)`). A guest that reads a file to EOF and later polls it again (a log tail loop written for FreeBSD, for example) sees new data here where it would see EOF on the console, and mixed reads can disagree (`fread` consumes the byte, `fscanf` returns it). Fix branch: https://github.com/husseinkad/AnyPS5/tree/fix/libc-eof-sticky (four commits on a recent main: the change, a macOS-safe call form, and two follow-ups from review; `libc/src/StandardStreams.cpp`, `FileAndHeap.cpp`, `Formatting.cpp`, `tests/GuestStandardStreams.cpp` with a new `CheckStickyEndOfFile`, and a debt entry). Those functions now return EOF while the host EOF flag is set; `fgets` with size 1 is exempt, as in FreeBSD. `fscanf` follows the first directive of its format: it returns 0 for an empty, whitespace-only or unknown-conversion format and EOF for a literal, `%%` or any conversion that needs input, matching FreeBSD's `vfscanf`. Checks: - `CheckStickyEndOfFile` fails on main (abort) and passes with the change; `guest_standard_streams` and `file_streams` pass. - Lockstep traces of about 2.6 million operations (20k seeds in each of two mixes) against a port of the FreeBSD 11.4 `refill`/`fgetc`/`fgets`/`fread`/`fseek`/`ungetc`/`clearerr` code, and 2,000 further read/EOF seeds against a semantic port with `fscanf`: zero EOF-related mismatches. The reported sequence (file `x\ny`; `fread(1x7)` returns 3; append `p`; `fread(5x1)` returns 0; `fscanf("%c")`) returns -1 as on FreeBSD. `fscanf` on stdin at EOF returns -1 and `getchar` keeps returning EOF; `sscanf`/`vsscanf` are unaffected; the format shapes `''`, `' '`, `'\n'`, `'%y'`, `'%%'`, `'x'`, `%c`, `%s`, `%[a]`, `%5c`, `%*d`, `' %d'` match `vfscanf.c`. - Three independent reviews. Two found problems that are fixed in the final commit (a stray NUL byte in a character literal and `fscanf` returning EOF for formats that read no input). `check_conventions.py` is clean, a byte scan of every changed file finds no NUL, and `pr_overlap.py` found no overlap by hand. Not covered (listed in `TechnicalDebt.md`): a format starting with `%n` can still read data appended after EOF (`"%n%d"` returns 1, FreeBSD returns -1); host `ungetc` limits (a second `ungetc` fails, `ungetc` then `rewind` returns the pushed byte, `ftello` is off by one after `ungetc` at offset 0); stdin is in text mode, so `ab\r\n` reads back as `ab\n`. The first commit's patch still contains the NUL byte that the last commit removes, so squash on merge. It conflicts with the separate failed-seek branch (#5251) only in two appended places (the test `main` and the debt file). Only Windows was built and run. AI-assisted: yes
MarcZX10 commented 2026-10-14 06:18:40 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T06:18:40Z.


Opened #5682 with the reviewed EOF-sticky fix, preserving Hussein Kadhim's commit authorship. On Linux GCC 12.2 Release, guest_standard_streams and file_streams pass (2/2), and conventions pass. The Windows-specific matrix from the issue report was not rerun locally; hosted CI is pending.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162221380 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5279#issuecomment-6162221380). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T06:18:40Z. --- Opened #5682 with the reviewed EOF-sticky fix, preserving Hussein Kadhim's commit authorship. On Linux GCC 12.2 Release, `guest_standard_streams` and `file_streams` pass (2/2), and conventions pass. The Windows-specific matrix from the issue report was not rerun locally; hosted CI is pending.
oneandonlydean commented 2026-10-14 06:54:44 +00:00 (Migrated from github.com)
Author
Contributor

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


Closing: no title is known to reach this, and functions are only changed when a title needs them. #5682 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:6162716967 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5279#issuecomment-6162716967). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T06:54:44Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. #5682 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#5279
No description provided.