libc: reads after end-of-file return newly appended data on Windows, where FreeBSD keeps returning EOF until clearerr; fix branch ready #5279
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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,freadandfscanfreturn bytes that were appended after EOF without aclearerr. FreeBSD 11.4refill.ckeeps returning EOF untilclearerr, a seek or anungetc(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 (freadconsumes the byte,fscanfreturns 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.cppwith a newCheckStickyEndOfFile, and a debt entry). Those functions now return EOF while the host EOF flag is set;fgetswith size 1 is exempt, as in FreeBSD.fscanffollows 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'svfscanf.Checks:
CheckStickyEndOfFilefails on main (abort) and passes with the change;guest_standard_streamsandfile_streamspass.refill/fgetc/fgets/fread/fseek/ungetc/clearerrcode, and 2,000 further read/EOF seeds against a semantic port withfscanf: zero EOF-related mismatches. The reported sequence (filex\ny;fread(1x7)returns 3; appendp;fread(5x1)returns 0;fscanf("%c")) returns -1 as on FreeBSD.fscanfon stdin at EOF returns -1 andgetcharkeeps returning EOF;sscanf/vsscanfare unaffected; the format shapes'',' ','\n','%y','%%','x',%c,%s,%[a],%5c,%*d,' %d'matchvfscanf.c.fscanfreturning EOF for formats that read no input).check_conventions.pyis clean, a byte scan of every changed file finds no NUL, andpr_overlap.pyfound no overlap by hand.Not covered (listed in
TechnicalDebt.md): a format starting with%ncan still read data appended after EOF ("%n%d"returns 1, FreeBSD returns -1); hostungetclimits (a secondungetcfails,ungetcthenrewindreturns the pushed byte,ftellois off by one afterungetcat offset 0); stdin is in text mode, soab\r\nreads back asab\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 testmainand the debt file). Only Windows was built and run.AI-assisted: yes
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_streamsandfile_streamspass (2/2), and conventions pass. The Windows-specific matrix from the issue report was not rerun locally; hosted CI is pending.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.