fix(libc): return 0, not EOF, from scanf when input ends after suppressed conversions #2766

Closed
husseinkad wants to merge 2 commits from upstream-pr/2766 into main
husseinkad commented 2026-10-10 13:27:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T13:27:43Z.


What

On Windows sscanf, vsscanf and fscanf returned EOF instead of 0 when the input ran out after only suppressed conversions had completed: sscanf("12", "%*d%d", &v) returned -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 too and ends with return (nconversions != 0 ? nassigned : EOF); %n and %*n are not conversions. The host scanf does not count the suppressed ones.

WindowsScanning.hpp now appends a private %n to each suppressed conversion (slots are heap allocated, one per %, so a format with hundreds of them is fine) and, when the host returns EOF but a slot was written, returns 0. Everything else is passed through unchanged. The Linux path hands the format to glibc, which returns EOF in this case; that and a related Windows host quirk (the host stops after the x of a 0x with no hex digit and after the e of an exponent with no digits, where C17 and FreeBSD stop before it) are recorded in TechnicalDebt and left alone, as fixing them needs per-directive scanning.

Tested

Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release:

  • guest_formatting (sscanf) and guest_standard_streams (fscanf) gain %*d%d on exhausted input (0), %*d on empty or blank input (still EOF), and the other forms; both are _WIN32-only because Linux keeps glibc's behaviour. They fail without the change (exit 0xc0000409) and pass with it. ctest -R "guest_formatting|guest_standard_stream|guest_scan_count|guest_strings" passes (4 of 4).
  • Outside the repo, the real guest exports of main's and this 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, vsscanf and fscanf through real streams): formats without %* are byte-identical to main in return value, assigned values and consumed length (19 million cases); with %* the only change from main is EOF becoming 0, 43.5 million times, and in all but 18 thousand of those it matches the reference. The remaining divergences from the reference are the host quirks above, which main has as well.
  • tools/check_conventions.py --base origin/main passes; tools/pr_overlap.py --branch reports no overlap.
  • Linux not run.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4811543758 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2766). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T13:27:43Z. --- ### What On Windows `sscanf`, `vsscanf` and `fscanf` returned `EOF` instead of 0 when the input ran out after only suppressed conversions had completed: `sscanf("12", "%*d%d", &v)` returned -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 too and ends with `return (nconversions != 0 ? nassigned : EOF)`; `%n` and `%*n` are not conversions. The host scanf does not count the suppressed ones. `WindowsScanning.hpp` now appends a private `%n` to each suppressed conversion (slots are heap allocated, one per `%`, so a format with hundreds of them is fine) and, when the host returns `EOF` but a slot was written, returns 0. Everything else is passed through unchanged. The Linux path hands the format to glibc, which returns `EOF` in this case; that and a related Windows host quirk (the host stops after the `x` of a `0x` with no hex digit and after the `e` of an exponent with no digits, where C17 and FreeBSD stop before it) are recorded in TechnicalDebt and left alone, as fixing them needs per-directive scanning. ### Tested Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release: - `guest_formatting` (`sscanf`) and `guest_standard_streams` (`fscanf`) gain `%*d%d` on exhausted input (0), `%*d` on empty or blank input (still `EOF`), and the other forms; both are `_WIN32`-only because Linux keeps glibc's behaviour. They fail without the change (exit `0xc0000409`) and pass with it. `ctest -R "guest_formatting|guest_standard_stream|guest_scan_count|guest_strings"` passes (4 of 4). - Outside the repo, the real guest exports of `main`'s and this 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, `vsscanf` and `fscanf` through real streams): formats without `%*` are byte-identical to `main` in return value, assigned values and consumed length (19 million cases); with `%*` the only change from `main` is `EOF` becoming 0, 43.5 million times, and in all but 18 thousand of those it matches the reference. The remaining divergences from the reference are the host quirks above, which `main` has as well. - `tools/check_conventions.py --base origin/main` passes; `tools/pr_overlap.py --branch` reports no overlap. - Linux not run. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-10 13:27:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T13:27:52Z.


Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6097987492 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2766#issuecomment-6097987492). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T13:27:52Z. --- Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

Pull request closed

Sign in to join this conversation.
No reviewers
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!2766
No description provided.