fix(libc): read %i without the C23 binary prefix in the scanf family on Linux #4358

Merged
milescoviello merged 1 commit from upstream-pr/4358 into main 2026-10-12 18:19:27 +00:00
milescoviello commented 2026-10-12 07:31:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T07:31:51Z.


What

On Linux, sscanf, vsscanf, fscanf and sscanf_s hand their conversions to glibc. Since glibc 2.38, code built as GNU C++ calls the C23 entry points (__isoc23_vsscanf, __isoc23_vfscanf, __isoc23_sscanf), whose %i reads a 0b/0B binary prefix. On main, sscanf("0b101 x", "%i%n%7s", &value, &end, rest) stores value 5 and end 5 and leaves x for the %s; " -0B11" gives -3. TechnicalDebt records this; #1459 fixed the same prefix in the strtol family and left scanf there.

FreeBSD's %i knows only the 0 and 0x prefixes. FreeBSD 11.4 lib/libc/stdio/vfscanf.c, parseint:

case '0':
	if (base == 0) {
		base = 8;
		flags |= PFXOK;
	}
...
/* letters ok iff hex */
case 'A': case 'B': case 'C':
case 'D': case 'E': case 'F':
case 'a': case 'b': case 'c':
case 'd': case 'e': case 'f':
	/* no need to fix base here */
	if (base <= 10)
		break;	/* not legal here */

so "0b101" stores 0, consumes the 0 and leaves b101, as C17 does and as the strtol family here already does.

With glibc, the scanf family now calls __isoc99_vsscanf and __isoc99_vfscanf: the entry points GNU C++ used before glibc 2.38 (exported since 2.7), which read %i without the binary prefix. Other hosts keep std::vsscanf and std::vfscanf. The TechnicalDebt entry is removed. No title is claimed.

Tested

Ubuntu 24.04 (glibc 2.39), GCC 13.3 (CI's compiler), Release with CI's configure flags.

  • guest_formatting checks sscanf("0b101", "%i%n%7s") (0, end 1, rest b101), vsscanf(" -0B11", "%i") (0), vsscanf_s("0b1", "%i") (0) and that sscanf("0x1f", "%i") still stores 31. guest_standard_streams checks fscanf on a stream holding 0b101 (0, next character b).
  • With only the tests applied to main's library sources both fail (guest_formatting: Formatting check failed; guest_standard_streams aborts). Before the fix the same calls store 5, -3 and 1 and consume the whole prefix.
  • With the fix both pass, as does guest_scan_count, and all 294 guest_* tests pass (-E '^guest_video_out_'). nm -D libc.prx shows __isoc99_vsscanf@GLIBC_2.7 and __isoc99_vfscanf@GLIBC_2.7 imported.

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 (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4826294081 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4358). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T07:31:51Z. --- ### What On Linux, `sscanf`, `vsscanf`, `fscanf` and `sscanf_s` hand their conversions to glibc. Since glibc 2.38, code built as GNU C++ calls the C23 entry points (`__isoc23_vsscanf`, `__isoc23_vfscanf`, `__isoc23_sscanf`), whose `%i` reads a `0b`/`0B` binary prefix. On main, `sscanf("0b101 x", "%i%n%7s", &value, &end, rest)` stores `value` 5 and `end` 5 and leaves `x` for the `%s`; `" -0B11"` gives -3. TechnicalDebt records this; #1459 fixed the same prefix in the `strtol` family and left `scanf` there. FreeBSD's `%i` knows only the `0` and `0x` prefixes. FreeBSD 11.4 [`lib/libc/stdio/vfscanf.c`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/stdio/vfscanf.c#L345-L380), `parseint`: ```c case '0': if (base == 0) { base = 8; flags |= PFXOK; } ... /* letters ok iff hex */ case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': /* no need to fix base here */ if (base <= 10) break; /* not legal here */ ``` so `"0b101"` stores 0, consumes the `0` and leaves `b101`, as C17 does and as the `strtol` family here already does. With glibc, the scanf family now calls `__isoc99_vsscanf` and `__isoc99_vfscanf`: the entry points GNU C++ used before glibc 2.38 (exported since 2.7), which read `%i` without the binary prefix. Other hosts keep `std::vsscanf` and `std::vfscanf`. The TechnicalDebt entry is removed. No title is claimed. ### Tested Ubuntu 24.04 (glibc 2.39), GCC 13.3 (CI's compiler), Release with CI's configure flags. - `guest_formatting` checks `sscanf("0b101", "%i%n%7s")` (0, end 1, rest `b101`), `vsscanf(" -0B11", "%i")` (0), `vsscanf_s("0b1", "%i")` (0) and that `sscanf("0x1f", "%i")` still stores 31. `guest_standard_streams` checks `fscanf` on a stream holding `0b101` (0, next character `b`). - With only the tests applied to main's library sources both fail (`guest_formatting`: `Formatting check failed`; `guest_standard_streams` aborts). Before the fix the same calls store 5, -3 and 1 and consume the whole prefix. - With the fix both pass, as does `guest_scan_count`, and all 294 `guest_*` tests pass (`-E '^guest_video_out_'`). `nm -D libc.prx` shows `__isoc99_vsscanf@GLIBC_2.7` and `__isoc99_vfscanf@GLIBC_2.7` imported. ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes
xXPBSXx commented 2026-10-12 13:20:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-12T13:20:28Z.


Windows test: head 89bc9d108 merged onto current main 0ef9dc928 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j4: full build 0 errors; ctest with the filter guest_formatting|guest_standard_stream|guest_wide_formatting: guest_wide_formatting passed, guest_standard_streams passed, guest_formatting passed (3 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change. The title names Linux, so this Windows run only shows that the change builds and the tests still pass on Windows.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6125117422 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4358#issuecomment-6125117422). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-12T13:20:28Z. --- Windows test: head `89bc9d108` merged onto current `main` `0ef9dc928` (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, `-j4`: full build 0 errors; ctest with the filter `guest_formatting|guest_standard_stream|guest_wide_formatting`: guest_wide_formatting passed, guest_standard_streams passed, guest_formatting passed (3 test(s), 0 failed); `python tools/check_conventions.py --base upstream/main` passed. I did not run the new tests against `main` without the change. The title names Linux, so this Windows run only shows that the change builds and the tests still pass on Windows. AI-assisted (Claude Code).
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!4358
No description provided.