fix(libc): wrap narrow integers in sscanf_s as titles' libc.prx does #12507

Merged
JJenkx merged 2 commits from upstream-pr/12507 into main 2026-11-05 11:05:03 +00:00
JJenkx commented 2026-10-28 16:26:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-28T16:26:44Z.


Refreshed against main 0263899e996f26b165aed37f6b5a616dd1d4c6bb; current head aa8c1b58be70ac9b5305a8998cbf724b6ea48d69 retains previous public head 0b3eb0b3e53a8faa1f01fd01a6de4061247ab308 and main as ancestors. The conflict resolution preserves the topic behavior and current-main ordinary-scan/input-failure handling and fixtures.

The same five fixtures (guest_formatting, guest_scan_count, guest_wide_stream_scan, guest_wide_formatting, guest_crt) pass three times on main and on this head: 15 executions per arm, zero failures/skips. Public-header audit, conventions and whitespace checks pass. These are actual scoped libc/kernel/Ampr owners and selected fixtures with existing frozen ZArchive/zstd providers, not a whole-project build. No new Windows, title-unblock or performance claim.

Raw build/test/JUnit logs, commands, source patches, conflict resolutions and binary hashes, SHA-256 fd8ddb6a38a010916d71698df9cb60011e9800fe14db63baa9f4eca21e6b8e4f (download verified). The batch has 87 successful fixture executions in total; setup failures and corrected final qualification are retained in the archive.

Previous report and contribution template, with historical pins preserved:

What

sscanf_s (and the other _s scanners that go through ScanSource: vsscanf_s, fscanf_s, vfscanf_s, scanf_s, vscanf_s) handed a narrow integer conversion to the host at the destination's width. On an out-of-range value the host saturates and sets ERANGE. Titles' own libc.prx (PPSA07923 and PPSA05916 agree) instead reads the field as a 64-bit value and keeps its low bits, leaving errno alone:

input format titles before this PR
61439ebdf %x 0x1439ebdf, errno unchanged 0xffffffff, ERANGE
9c747bca9c %x 0x747bca9c 0xffffffff, ERANGE
-eb371ade9 %hx 0x5217 0xffff, ERANGE
867259ccd %hhX 0xcd 0xff, ERANGE

Plain sscanf on Windows already does this: ScanWindowsArguments scans narrow integers as ll and truncates. ScanSource now does the same for %d, %i, %o, %u, %x and %X with no length, h or hh: it scans into a 64-bit temporary and stores the destination's low bytes. Past 64 bits the host still saturates with ERANGE, as the titles do (%d of 99999999999999999999 sets ERANGE in both).

Tested

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, on main 6793a72475.

  • Against titles' own libc.prx: I loaded PPSA07923's and PPSA05916's libc.prx natively and called their sscanf_s (24m4Z4bUaoY) side by side with this build. It covered 200,000 random numeric conversions: d i u o x X f e g a, every length, random widths, and inputs drawn from digits, hex letters, signs, ., x, e, p, inf/nan letters and spaces. I compared the result, the stored bytes, %n and errno.
    • narrow-integer differences: main 21 → 0, on both titles;
    • what's left is unrelated to this change and listed in the debt file or measured separately:
      • 0x with no hex digit (78, where the titles fail the conversion);
      • out-of-range %f/hex %a errno (37, where the titles leave errno);
      • one long double NaN's upper bytes.
  • Tests: guest_formatting (three new rows), guest_stdio_variadic, guest_scan_count, guest_wide_stream_scan and guest_wide_formatting pass in each of 3 runs.
  • Negative control: with main's Formatting.cpp, guest_formatting fails.

Not tested on Linux or macOS. ScanSource is the same code there, so Linux sscanf_s now also scans these conversions as 64-bit and truncates. I haven't measured what glibc did with them before.

Overlap: tools/pr_overlap.py finds no shared lines: the 14 pull requests touching these files merge cleanly with this one. #5021, #7386, #10324 and #11332 conflict with main and couldn't be compared.

Independent Windows validation reported by bunbeans2-arch for topic 0b3eb0b3e53a8faa1f01fd01a6de4061247ab308 merged into main 409fe64199: Windows 11 Pro 22631, i9-9900K, MSYS2 UCRT64 GCC 16.1, Release. guest_formatting passes 5/5; retaining this PR's tests while restoring Formatting.cpp to main fails 5/5; plain main with its own source/tests passes 3/3. This is the contributor's run, not a new local Windows execution.

Independent reports on this head (their runs, summarised here):

  • @bunbeans2-arch (report): Windows 11 Pro, MSYS2 UCRT64 GCC 16.1.0, head 0b3eb0b3e5 merged into main 409fe64199: guest_formatting passes 5/5; with Formatting.cpp from main it fails 5/5.

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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5053970649 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12507). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-28T16:26:44Z. --- <!-- codex-format-main026-20261104 --> Refreshed against main `0263899e996f26b165aed37f6b5a616dd1d4c6bb`; current head `aa8c1b58be70ac9b5305a8998cbf724b6ea48d69` retains previous public head `0b3eb0b3e53a8faa1f01fd01a6de4061247ab308` and main as ancestors. The conflict resolution preserves the topic behavior and current-main ordinary-scan/input-failure handling and fixtures. The same five fixtures (`guest_formatting`, `guest_scan_count`, `guest_wide_stream_scan`, `guest_wide_formatting`, `guest_crt`) pass three times on main and on this head: 15 executions per arm, zero failures/skips. Public-header audit, conventions and whitespace checks pass. These are actual scoped libc/kernel/Ampr owners and selected fixtures with existing frozen ZArchive/zstd providers, not a whole-project build. No new Windows, title-unblock or performance claim. [Raw build/test/JUnit logs, commands, source patches, conflict resolutions and binary hashes](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-libc-format4-main026-linux-20261104.tar.gz), SHA-256 `fd8ddb6a38a010916d71698df9cb60011e9800fe14db63baa9f4eca21e6b8e4f` (download verified). The batch has 87 successful fixture executions in total; setup failures and corrected final qualification are retained in the archive. Previous report and contribution template, with historical pins preserved: ### What `sscanf_s` (and the other `_s` scanners that go through `ScanSource`: `vsscanf_s`, `fscanf_s`, `vfscanf_s`, `scanf_s`, `vscanf_s`) handed a narrow integer conversion to the host at the destination's width. On an out-of-range value the host saturates and sets `ERANGE`. Titles' own `libc.prx` (PPSA07923 and PPSA05916 agree) instead reads the field as a 64-bit value and keeps its low bits, leaving `errno` alone: | input | format | titles | before this PR | |---|---|---|---| | `61439ebdf` | `%x` | `0x1439ebdf`, `errno` unchanged | `0xffffffff`, `ERANGE` | | `9c747bca9c` | `%x` | `0x747bca9c` | `0xffffffff`, `ERANGE` | | `-eb371ade9` | `%hx` | `0x5217` | `0xffff`, `ERANGE` | | `867259ccd` | `%hhX` | `0xcd` | `0xff`, `ERANGE` | Plain `sscanf` on Windows already does this: `ScanWindowsArguments` scans narrow integers as `ll` and truncates. `ScanSource` now does the same for `%d`, `%i`, `%o`, `%u`, `%x` and `%X` with no length, `h` or `hh`: it scans into a 64-bit temporary and stores the destination's low bytes. Past 64 bits the host still saturates with `ERANGE`, as the titles do (`%d` of `99999999999999999999` sets `ERANGE` in both). ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, on `main` `6793a72475`. - **Against titles' own `libc.prx`:** I loaded PPSA07923's and PPSA05916's `libc.prx` natively and called their `sscanf_s` (`24m4Z4bUaoY`) side by side with this build. It covered 200,000 random numeric conversions: `d i u o x X f e g a`, every length, random widths, and inputs drawn from digits, hex letters, signs, `.`, `x`, `e`, `p`, `inf`/`nan` letters and spaces. I compared the result, the stored bytes, `%n` and `errno`. - **narrow-integer differences:** `main` 21 → **0**, on both titles; - **what's left** is unrelated to this change and listed in the debt file or measured separately: - `0x` with no hex digit (78, where the titles fail the conversion); - out-of-range `%f`/hex `%a` `errno` (37, where the titles leave `errno`); - one `long double` NaN's upper bytes. - **Tests:** `guest_formatting` (three new rows), `guest_stdio_variadic`, `guest_scan_count`, `guest_wide_stream_scan` and `guest_wide_formatting` pass in each of 3 runs. - **Negative control:** with `main`'s `Formatting.cpp`, `guest_formatting` fails. Not tested on Linux or macOS. `ScanSource` is the same code there, so Linux `sscanf_s` now also scans these conversions as 64-bit and truncates. I haven't measured what glibc did with them before. **Overlap:** `tools/pr_overlap.py` finds no shared lines: the 14 pull requests touching these files merge cleanly with this one. #5021, #7386, #10324 and #11332 conflict with `main` and couldn't be compared. Independent Windows validation reported by [bunbeans2-arch](https://github.com/boykopovar/AnyPS5/pull/12507#issuecomment-6519255586) for topic `0b3eb0b3e53a8faa1f01fd01a6de4061247ab308` merged into main `409fe64199`: Windows 11 Pro 22631, i9-9900K, MSYS2 UCRT64 GCC 16.1, Release. `guest_formatting` passes 5/5; retaining this PR's tests while restoring `Formatting.cpp` to main fails 5/5; plain main with its own source/tests passes 3/3. This is the contributor's run, not a new local Windows execution. **Independent reports** on this head (their runs, summarised here): - @bunbeans2-arch ([report](https://github.com/boykopovar/AnyPS5/pull/12507#issuecomment-6519255586)): Windows 11 Pro, MSYS2 UCRT64 GCC 16.1.0, head `0b3eb0b3e5` merged into `main` `409fe64199`: `guest_formatting` passes 5/5; with `Formatting.cpp` from `main` it fails 5/5. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-10-28 16:41:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T16:41:48Z.

Review: APPROVED


Reviewed exact head 0b3eb0b3e53a8faa1f01fd01a6de4061247ab308. For unsuppressed integer conversions with no length, h, or hh, the change scans through a 64-bit temporary and copies only the destination-width low bytes, avoiding the host-width saturation observed for narrower destinations. The tests cover %x, %hx, %hhX, and preservation of the preexisting errno value; the PR also supplies paired title-libc comparisons across 200,000 conversions. Windows, conventions, policy, Ubuntu relinker/register-context and ASan checks pass. Linux is skipped and macOS jobs are pending; the PR description clearly records that host limitation and the remaining >64-bit overflow behavior.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5686314487 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12507#pullrequestreview-5686314487). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T16:41:48Z. **Review:** APPROVED --- Reviewed exact head `0b3eb0b3e53a8faa1f01fd01a6de4061247ab308`. For unsuppressed integer conversions with no length, `h`, or `hh`, the change scans through a 64-bit temporary and copies only the destination-width low bytes, avoiding the host-width saturation observed for narrower destinations. The tests cover `%x`, `%hx`, `%hhX`, and preservation of the preexisting errno value; the PR also supplies paired title-libc comparisons across 200,000 conversions. Windows, conventions, policy, Ubuntu relinker/register-context and ASan checks pass. Linux is skipped and macOS jobs are pending; the PR description clearly records that host limitation and the remaining >64-bit overflow behavior.
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!12507
No description provided.