libc: sscanf, vsscanf and fscanf return EOF instead of 0 on Windows when input ends after only suppressed conversions #3067
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-10T21:32:53Z.
What
On Windows
sscanf,vsscanfandfscanf(core/libs/prx/libc/include/WindowsScanning.hpp) returnEOFinstead of 0 when the input runs out after only suppressed (%*) conversions have completed:sscanf("12", "%*d%d", &v)returns -1. C17 7.21.6.2p16 returnsEOFonly if an input failure occurs before the first conversion has completed, and FreeBSD'svfscanf.c(releng/11.4, lines 799 to 806) incrementsnconversionsfor suppressed conversions as well and ends withreturn (nconversions != 0 ? nassigned : EOF);%nand%*nare not conversions. The host scanf does not count the suppressed ones, so code that tells "nothing matched at all" (EOF) from "matched, but no later field" (0) gets the wrong answer.Expected on FreeBSD and C17:
sscanf("12","%*d%d")returns 0,sscanf("12","%*d")returns 0,sscanf("","%*d")andsscanf(" ","%*d")returnEOF,sscanf("x","%*d")returns 0 (matching failure). Linux hands the format to glibc, which returnsEOFin the first case too (documented in TechnicalDebt).Fix
A tested fix is on my fork:
husseinkad:fix/libc-scanf-suppressed-eof(three commits onmain; PR #2766, closed by the intake check). It appends a private%nafter each suppressed conversion (heap slots, one per%, so hundreds of suppressed conversions are fine) and returns 0 when the host returnsEOFbut a slot was written; every other result is untouched.guest_formatting(sscanf) andguest_standard_streams(fscanf) test the cases above on Windows and fail without the change.Checked with the real guest exports of
main's and the branch'slibc.prxagainst a reference written from C17 and FreeBSD'svfscanf: about 273 million format/input pairs (all formats of up to four directives over%*d %d %*c %c %*s %s %*[a-c] %[a-c] %*x %n %*5d %*ld %*lf %*i %*[^,] %%against inputs of up to five characters, a million random formats with length modifiers and scansets, 300 to 5,000 suppressed conversions in one format,fscanfthrough real streams): formats without%*are byte-identical tomain(19 million cases); with%*the only change frommainisEOFbecoming 0, 43.5 million times, and all but 18 thousand of those match the reference. The remaining divergences are a separate host quirk thatmainhas as well: the host stops after thexof a0xwith no hex digit (%x,%X,%i,%p) and after theeof an exponent with no digits (%e,%f,%g,%a), where C17 and FreeBSD stop before it; that needs per-directive scanning and is only recorded in TechnicalDebt. Linux not run.AI-assisted: yes.