libc: sscanf, vsscanf and fscanf return EOF instead of 0 on Windows when input ends after only suppressed conversions #3067

Closed
opened 2026-10-10 21:32:53 +00:00 by husseinkad · 0 comments
husseinkad commented 2026-10-10 21:32:53 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T21:32:53Z.


What

On Windows sscanf, vsscanf and fscanf (core/libs/prx/libc/include/WindowsScanning.hpp) return EOF instead 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 returns EOF only if an input failure occurs before the first conversion has completed, and FreeBSD's vfscanf.c (releng/11.4, lines 799 to 806) increments nconversions for suppressed conversions as well and ends with return (nconversions != 0 ? nassigned : EOF); %n and %*n are 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") and sscanf(" ","%*d") return EOF, sscanf("x","%*d") returns 0 (matching failure). Linux hands the format to glibc, which returns EOF in the first case too (documented in TechnicalDebt).

Fix

A tested fix is on my fork: husseinkad:fix/libc-scanf-suppressed-eof (three commits on main; PR #2766, closed by the intake check). It appends a private %n after each suppressed conversion (heap slots, one per %, so hundreds of suppressed conversions are fine) and returns 0 when the host returns EOF but a slot was written; every other result is untouched. guest_formatting (sscanf) and guest_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's libc.prx against a reference written from C17 and FreeBSD's vfscanf: 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, fscanf through real streams): formats without %* are byte-identical to main (19 million cases); with %* the only change from main is EOF becoming 0, 43.5 million times, and all but 18 thousand of those match the reference. The remaining divergences are a separate host quirk that main has as well: the host stops after the x of a 0x with no hex digit (%x, %X, %i, %p) and after the e of 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5795464742 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3067). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T21:32:53Z. --- ### What On Windows `sscanf`, `vsscanf` and `fscanf` (`core/libs/prx/libc/include/WindowsScanning.hpp`) return `EOF` instead 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 returns `EOF` only if an input failure occurs before the first conversion has completed, and FreeBSD's [`vfscanf.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/stdio/vfscanf.c) (releng/11.4, lines 799 to 806) increments `nconversions` for suppressed conversions as well and ends with `return (nconversions != 0 ? nassigned : EOF)`; `%n` and `%*n` are 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")` and `sscanf(" ","%*d")` return `EOF`, `sscanf("x","%*d")` returns 0 (matching failure). Linux hands the format to glibc, which returns `EOF` in the first case too (documented in TechnicalDebt). ### Fix A tested fix is on my fork: [`husseinkad:fix/libc-scanf-suppressed-eof`](https://github.com/husseinkad/AnyPS5/tree/fix/libc-scanf-suppressed-eof) (three commits on `main`; PR #2766, closed by the intake check). It appends a private `%n` after each suppressed conversion (heap slots, one per `%`, so hundreds of suppressed conversions are fine) and returns 0 when the host returns `EOF` but a slot was written; every other result is untouched. `guest_formatting` (`sscanf`) and `guest_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's `libc.prx` against a reference written from C17 and FreeBSD's `vfscanf`: 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, `fscanf` through real streams): formats without `%*` are byte-identical to `main` (19 million cases); with `%*` the only change from `main` is `EOF` becoming 0, 43.5 million times, and all but 18 thousand of those match the reference. The remaining divergences are a separate host quirk that `main` has as well: the host stops after the `x` of a `0x` with no hex digit (`%x`, `%X`, `%i`, `%p`) and after the `e` of 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.
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#3067
No description provided.