fix(libc): fail a 0x with no hex digit and keep errno for in-range floats in sscanf_s #12530

Merged
JJenkx merged 3 commits from upstream-pr/12530 into main 2026-11-06 12:25:58 +00:00
JJenkx commented 2026-10-28 17:53:44 +00:00 (Migrated from github.com)

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


Refreshed against main 0263899e996f26b165aed37f6b5a616dd1d4c6bb; current head af0443d37194a7000804f22c1e71c82cae572dbd retains previous public head 3eca2be06731a86e6dc6a7cf07259dc1378eb149 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:

Stacked on #12507. It builds on #12507's change to the same lines of ScanSource; review the second commit, 3eca2be067.

What

sscanf_s and vsscanf_s now handle two cases the way the libc.prx that titles ship does.

1. A 0x prefix with no hex digit after it. main handed these fields to the host scanner, which takes 0x as 0 and counts a successful conversion. The titles fail the conversion and store nothing:

field titles' result
%x, %X, %i or %p where the input or the width ends right after the x, with no sign ("0x", " 0X", "0x1" with %2x) input failure: EOF, or the count so far after an earlier conversion
the same with a non-hex character after the x ("0xg"), or with a sign ("-0x", "-0x1" with %3x) matching failure
floating (%a, %e, %f, %g) needs a hex digit, or . and a hex digit, within the width ("0x.8" is 0.5); anything else is a matching failure, never EOF

%d, %o and %u read the 0 and are unchanged. The check runs before the field reaches the host scanner.

2. errno for floating fields. UCRT sets ERANGE when %e of 1e40 overflows a float, and when %a of 0x1p200 does. The titles set it only when a decimal field overflows, or underflows to 0, as a long double, whatever the destination width; otherwise errno is left alone. This is the same rule #12514 measured for swscanf. Examples:

  • %Le of 1e5000 and %Lg of 1e-5000 set ERANGE;
  • %le of 1e400 and %La of 0x1p20000 don't.

errno is now decided from the scanned text with strtold.

Limits (new debt entry):

  • The stream forms (fscanf_s, scanf_s and their v forms) can only look one character ahead, so they keep the old behaviour.
  • On a host whose long double is double (macOS on ARM), a decimal field beyond double range sets ERANGE.

Tested

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, head 3eca2be067 (on #12507, base 6793a72475).

  • Native comparison: a scratch loader runs the decrypted libc.prx of PPSA07923 and PPSA05916 and calls their sscanf_s by NID.
    • Random: 200,000 random numeric conversions per title, comparing the result, stored bytes, %n and errno. main (470a562903) has 136 differences in PPSA07923: 78 0x failures, 37 float errno cases, #12507's 20 wrap cases and the NaN case below. This PR has 1 per title: a %5Lf of "-naNXf" that differs past the 8 bytes the probe prints, most likely the NaN's sign. That is a separate issue and isn't touched here.
    • Targeted: 33 prefix cases (signs, widths, %i, %p, suppressed fields, a failure after an earlier conversion) and 15 range cases (%e, %le, %Le, %a, %La, %Lg, 64-bit integer overflow). 0 differences in both titles.
  • Unit tests: guest_formatting gains these cases. With #12507's Formatting.cpp and the new tests, guest_formatting fails.
  • Regression set: guest_formatting, guest_stdio_variadic, guest_scan_count, guest_wide_stream_scan, guest_wide_formatting, file_streams and the five guest_standard_streams* tests pass 3/3. Conventions pass.
  • Merges: git merge-tree merges this head cleanly with #11852, #12428 and #12468, the other open PRs that touch Formatting.cpp.
  • Linux/macOS: covered by CI only; I didn't run them locally.

Integration and landing order — 2026-10-29

For my own PRs, the landing order is #12507 → #12530 → #12468. #12468 now retains StringSource::Remaining(), which #12530 uses twice. A private tree exactly matching the merge of #12468 accessor fix a84100962ceb61a1646c04aa62bdcc254c8cec01 and this PR at 3eca2be06731a86e6dc6a7cf07259dc1378eb149 builds and passes five focused Linux suites three times (15 checks). Removing the accessor produces the expected compile errors at those two calls; restoration passes all15 checks and restores identical libc bytes. The private combined source was not published.

The final #12468 head 068725777be07527eda51e086d8850500b7b8707 additionally integrates main157b and passes the same15 checks, with both formatting translation units passing MinGW cross syntax checks. This does not constitute new Windows runtime testing or external #11724 author agreement.

Raw qualification bundle, SHA-256 ca6b35926bcc6d3f21a9650517894c86b65594e2e2b653c5c32cd1c1de89c835.

AI-assisted: yes (Codex).

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

  • @bunbeans2-arch (report): Windows 11 Pro, MSYS2 UCRT64 GCC 16.1.0, head 3eca2be067 merged into main fae9e483bb: 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: #12507
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5055177241 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12530). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-28T17:53:44Z. --- <!-- codex-format-main026-20261104 --> Refreshed against main `0263899e996f26b165aed37f6b5a616dd1d4c6bb`; current head `af0443d37194a7000804f22c1e71c82cae572dbd` retains previous public head `3eca2be06731a86e6dc6a7cf07259dc1378eb149` 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: **Stacked on #12507.** It builds on #12507's change to the same lines of `ScanSource`; review the second commit, `3eca2be067`. ### What `sscanf_s` and `vsscanf_s` now handle two cases the way the `libc.prx` that titles ship does. **1. A `0x` prefix with no hex digit after it.** `main` handed these fields to the host scanner, which takes `0x` as 0 and counts a successful conversion. The titles fail the conversion and store nothing: | field | titles' result | |---|---| | `%x`, `%X`, `%i` or `%p` where the input or the width ends right after the `x`, with no sign (`"0x"`, `" 0X"`, `"0x1"` with `%2x`) | input failure: `EOF`, or the count so far after an earlier conversion | | the same with a non-hex character after the `x` (`"0xg"`), or with a sign (`"-0x"`, `"-0x1"` with `%3x`) | matching failure | | floating (`%a`, `%e`, `%f`, `%g`) | needs a hex digit, or `.` and a hex digit, within the width (`"0x.8"` is 0.5); anything else is a matching failure, never `EOF` | `%d`, `%o` and `%u` read the `0` and are unchanged. The check runs before the field reaches the host scanner. **2. `errno` for floating fields.** UCRT sets `ERANGE` when `%e` of `1e40` overflows a `float`, and when `%a` of `0x1p200` does. The titles set it only when a *decimal* field overflows, or underflows to 0, as a `long double`, whatever the destination width; otherwise `errno` is left alone. This is the same rule #12514 measured for `swscanf`. Examples: - `%Le` of `1e5000` and `%Lg` of `1e-5000` set `ERANGE`; - `%le` of `1e400` and `%La` of `0x1p20000` don't. `errno` is now decided from the scanned text with `strtold`. **Limits** (new debt entry): - The stream forms (`fscanf_s`, `scanf_s` and their `v` forms) can only look one character ahead, so they keep the old behaviour. - On a host whose `long double` is `double` (macOS on ARM), a decimal field beyond `double` range sets `ERANGE`. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, head `3eca2be067` (on #12507, base `6793a72475`). - **Native comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923 and PPSA05916 and calls their `sscanf_s` by NID. - **Random:** 200,000 random numeric conversions per title, comparing the result, stored bytes, `%n` and `errno`. `main` (`470a562903`) has 136 differences in PPSA07923: 78 `0x` failures, 37 float `errno` cases, #12507's 20 wrap cases and the NaN case below. This PR has **1** per title: a `%5Lf` of `"-naNXf"` that differs past the 8 bytes the probe prints, most likely the NaN's sign. That is a separate issue and isn't touched here. - **Targeted:** 33 prefix cases (signs, widths, `%i`, `%p`, suppressed fields, a failure after an earlier conversion) and 15 range cases (`%e`, `%le`, `%Le`, `%a`, `%La`, `%Lg`, 64-bit integer overflow). 0 differences in both titles. - **Unit tests:** `guest_formatting` gains these cases. With #12507's `Formatting.cpp` and the new tests, `guest_formatting` fails. - **Regression set:** `guest_formatting`, `guest_stdio_variadic`, `guest_scan_count`, `guest_wide_stream_scan`, `guest_wide_formatting`, `file_streams` and the five `guest_standard_streams*` tests pass 3/3. Conventions pass. - **Merges:** `git merge-tree` merges this head cleanly with #11852, #12428 and #12468, the other open PRs that touch `Formatting.cpp`. - **Linux/macOS:** covered by CI only; I didn't run them locally. <!-- codex-maintenance-20261029-repeat-order-12530 --> **Integration and landing order — 2026-10-29** For my own PRs, the landing order is #12507 → #12530 → #12468. #12468 now retains `StringSource::Remaining()`, which #12530 uses twice. A private tree exactly matching the merge of #12468 accessor fix `a84100962ceb61a1646c04aa62bdcc254c8cec01` and this PR at `3eca2be06731a86e6dc6a7cf07259dc1378eb149` builds and passes five focused Linux suites three times (15 checks). Removing the accessor produces the expected compile errors at those two calls; restoration passes all15 checks and restores identical libc bytes. The private combined source was not published. The final #12468 head `068725777be07527eda51e086d8850500b7b8707` additionally integrates main157b and passes the same15 checks, with both formatting translation units passing MinGW cross syntax checks. This does not constitute new Windows runtime testing or external #11724 author agreement. [Raw qualification bundle](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/cpu-complete-current-main-linux-20261029.tar.gz), SHA-256 `ca6b35926bcc6d3f21a9650517894c86b65594e2e2b653c5c32cd1c1de89c835`. AI-assisted: yes (Codex). **Independent reports** on this head (their runs, summarised here): - @bunbeans2-arch ([report](https://github.com/boykopovar/AnyPS5/pull/12530#issuecomment-6540087929)): Windows 11 Pro, MSYS2 UCRT64 GCC 16.1.0, head `3eca2be067` merged into `main` `fae9e483bb`: `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: #12507 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-10-28 18:27:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T18:27:08Z.

Review: APPROVED


Reviewed — looks good

The second stacked commit’s prefix checks and string-scan errno handling match the measured behavior described in the PR, and I found no blocking issue in that incremental diff.

What I ran

Reviewed commit 3eca2be06731a86e6dc6a7cf07259dc1378eb149 on parent PR #12507 head 0b3eb0b3e53a8faa1f01fd01a6de4061247ab308. The cumulative head merges cleanly onto current main f21ef7c99c5af648c5a41bddb18ce118b8e60c46. Environment: Debian 12 x86_64. I checked the incremental diff, merge tree, and whitespace; I did not build or execute the formatting tests locally.

git diff pr/12507...pr/12530 -- core/libs/prx/libc/src/Formatting.cpp core/libs/tests/Formatting.cpp docs/dev/debt/libc.md
git diff --check pr/12507...pr/12530
git merge-tree $(git merge-base upstream/main pr/12530) upstream/main pr/12530
gh pr checks 12530 -R boykopovar/AnyPS5

Results

Check Result
Incremental diff from #12507 Reviewed; limited to prefix parsing, floating range errno, tests, and debt documentation
Merge onto current main Clean
git diff --check Pass; no output
Conventions, eligibility, intake, labels Pass
Hosted Windows job Pass; selected-test, build, and CTest steps skipped
Linux Skipped
Focused differential and regression tests Author reports targeted comparisons matched both title libc builds and the regression set passed 3/3; not independently rerun by me
macOS jobs Pending when checked
Command output
$ git diff --check pr/12507...pr/12530
(no output)

$ git merge-tree $(git merge-base upstream/main pr/12530) upstream/main pr/12530 | head -3
merged
  result 100644 464536c25d2f7b531cd37b16a664125621c0933f core/libs/prx/libc/src/Formatting.cpp
  our    100644 ac280da5abaf8689e7817e9507167dfd17e18ad1 core/libs/prx/libc/src/Formatting.cpp

$ gh pr checks 12530 -R boykopovar/AnyPS5
conventions pass
eligibility pass
intake pass
labels pass
register-context (ubuntu-latest, Debug) pass
register-context (ubuntu-latest, Release) pass
relinker (ubuntu-latest, g++) pass
relinker (windows-latest, g++) pass
windows pass
relinker-asan pass
report pass
register-context (macos-15-intel, Debug) pending
register-context (macos-15-intel, Release) pending
relinker (macos-latest, clang++) pending
relinker (macos-latest, clang++, x86_64) pending
write-watch (macos-15) pending
write-watch (macos-15-intel) pending
linux skipping

Negative control

Assertion Change under test Did the new test fail?
Prefix failures and in-range floating conversions retain the measured behavior Not independently run by me; author reports guest_formatting fails with #12507’s prior implementation Author reports yes; not independently reproduced

Findings

Blocking

None found in the incremental change.

Non-blocking

The focused tests and title comparisons are author-reported. The hosted Windows job skipped CTest; Linux was skipped and macOS checks were pending when checked.

Sources

Not verified

I did not independently build/run the tests, use the title comparison loader, test on macOS or a console, or rerun the author’s random/targeted probes.

To get this to a good state

No follow-up is needed for this incremental review. If the commit changes, I will re-review its diff and rerun the merge and whitespace checks.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5687694191 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12530#pullrequestreview-5687694191). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T18:27:08Z. **Review:** APPROVED --- # Reviewed — looks good The second stacked commit’s prefix checks and string-scan `errno` handling match the measured behavior described in the PR, and I found no blocking issue in that incremental diff. ### What I ran Reviewed commit `3eca2be06731a86e6dc6a7cf07259dc1378eb149` on parent PR #12507 head `0b3eb0b3e53a8faa1f01fd01a6de4061247ab308`. The cumulative head merges cleanly onto current `main` `f21ef7c99c5af648c5a41bddb18ce118b8e60c46`. Environment: Debian 12 x86_64. I checked the incremental diff, merge tree, and whitespace; I did not build or execute the formatting tests locally. ```sh git diff pr/12507...pr/12530 -- core/libs/prx/libc/src/Formatting.cpp core/libs/tests/Formatting.cpp docs/dev/debt/libc.md git diff --check pr/12507...pr/12530 git merge-tree $(git merge-base upstream/main pr/12530) upstream/main pr/12530 gh pr checks 12530 -R boykopovar/AnyPS5 ``` ### Results | Check | Result | |---|---| | Incremental diff from #12507 | Reviewed; limited to prefix parsing, floating range `errno`, tests, and debt documentation | | Merge onto current `main` | Clean | | `git diff --check` | Pass; no output | | Conventions, eligibility, intake, labels | Pass | | Hosted Windows job | Pass; selected-test, build, and CTest steps skipped | | Linux | Skipped | | Focused differential and regression tests | Author reports targeted comparisons matched both title libc builds and the regression set passed 3/3; not independently rerun by me | | macOS jobs | Pending when checked | <details> <summary>Command output</summary> ```text $ git diff --check pr/12507...pr/12530 (no output) $ git merge-tree $(git merge-base upstream/main pr/12530) upstream/main pr/12530 | head -3 merged result 100644 464536c25d2f7b531cd37b16a664125621c0933f core/libs/prx/libc/src/Formatting.cpp our 100644 ac280da5abaf8689e7817e9507167dfd17e18ad1 core/libs/prx/libc/src/Formatting.cpp $ gh pr checks 12530 -R boykopovar/AnyPS5 conventions pass eligibility pass intake pass labels pass register-context (ubuntu-latest, Debug) pass register-context (ubuntu-latest, Release) pass relinker (ubuntu-latest, g++) pass relinker (windows-latest, g++) pass windows pass relinker-asan pass report pass register-context (macos-15-intel, Debug) pending register-context (macos-15-intel, Release) pending relinker (macos-latest, clang++) pending relinker (macos-latest, clang++, x86_64) pending write-watch (macos-15) pending write-watch (macos-15-intel) pending linux skipping ``` </details> ### Negative control | Assertion | Change under test | Did the new test fail? | |---|---|---| | Prefix failures and in-range floating conversions retain the measured behavior | Not independently run by me; author reports `guest_formatting` fails with #12507’s prior implementation | Author reports yes; not independently reproduced | ### Findings #### Blocking None found in the incremental change. #### Non-blocking The focused tests and title comparisons are author-reported. The hosted Windows job skipped CTest; Linux was skipped and macOS checks were pending when checked. ### Sources - [Prefix and range handling at the reviewed commit](https://github.com/boykopovar/AnyPS5/blob/3eca2be06731a86e6dc6a7cf07259dc1378eb149/core/libs/prx/libc/src/Formatting.cpp#L138-L161) - [Focused tests at the reviewed commit](https://github.com/boykopovar/AnyPS5/blob/3eca2be06731a86e6dc6a7cf07259dc1378eb149/core/libs/tests/Formatting.cpp#L332-L379) - [Documented behavior and platform limitation at the reviewed commit](https://github.com/boykopovar/AnyPS5/blob/3eca2be06731a86e6dc6a7cf07259dc1378eb149/docs/dev/debt/libc.md#L103) ### Not verified I did not independently build/run the tests, use the title comparison loader, test on macOS or a console, or rerun the author’s random/targeted probes. ### To get this to a good state No follow-up is needed for this incremental review. If the commit changes, I will re-review its diff and rerun the merge and whitespace checks.
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!12530
No description provided.