fix(libc): leave errno alone for out-of-range float and double in swscanf #9973

Merged
JJenkx merged 2 commits from upstream-pr/9973 into main 2026-10-26 15:20:09 +00:00
JJenkx commented 2026-10-23 01:26:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T01:26:17Z.


What

The wide scanf family (swscanf and the stream forms that run its scanner) converts %f, %e, %g and %a with strtof, strtod or strtold. Those set ERANGE when the value overflows or underflows. The libc.prx that titles ship stores the same infinity or zero, but sets ERANGE only for a long double:

Input Format Bundled-library errno Windows main errno
1e39, 1e-50 %f, %e unchanged ERANGE
1e400, 1e-400 %lf unchanged ERANGE
1e5000, 1e-5000, 1e4933 %Lf, %Le ERANGE ERANGE
1e-4950 (a representable long double) %Lf unchanged unchanged

The narrow sscanf of the same copies gives the same errno in all these cases. This PR restores errno after the float and double conversions. The long double one keeps strtold's ERANGE. Values are unchanged.

Remaining gaps. Narrow sscanf delegates floating conversions to host libc on both Linux and Windows. Out-of-range %f/%e and %lf can therefore still leak host ERANGE; this PR fixes the wide scanner only. The unchanged strtold path also differs between hosts: on Linux/glibc 2.44, 1e-4950 sets ERANGE, including in the host narrow %Lf scan, whereas the Windows and bundled-library results in the table leave it unchanged. Fixing these gaps requires per-field errno handling while retaining legitimate range errors from other conversions in the same call.

Tested

Current reviewer-requested Linux refresh (2026-10-25): exact head c228d7a459cea0e7dca1f8972915786c14d23d64 directly on main 3a930342d5a2189b78643818f04b8948fc4c3dc9, two topic commits without merge commits. The entire production scanner is current main plus the original Float method and <cerrno> include; that method and the original eleven-line regression block are unchanged from previous head 93d553448df31ae614cfd28f9eedc9378fac0648. Main's canonical Api.hpp include, newer %S/%C matching-failure behavior and all its other scanner/test changes are retained.

The debt resolution preserves every current-main row and records the confirmed wide float/double errno contract plus both known remaining gaps in a separate concise floating scan errno entry. It keeps main's newer scanner descriptions rather than restoring the older combined paragraph.

Fresh Linux x86-64 Release libc and all eight scanner test executables built. The eight scanner groups from the earlier Linux report plus public_headers each pass three runs (27 passes). Keeping candidate tests and substituting only exact main production ScanningWide.cpp makes guest_wide_conversion fail on the three added float/double overflow/underflow errno checks; the other eight groups pass 24/24. Restored candidate gives 27 passes and a byte-identical raw libc.prx. Conventions, whitespace and merge-tree checks pass.

Exact-head logs, main-source negative control and complete-file preservation proof (SHA-256 b3ddb52a9674c73bd32396624e26dca3923b9c92c118f95941b335c1556a76c7). This directly addresses the request to refresh main while retaining the errno cases and documented gaps. Earlier Windows and module differential results below stay pinned to their measured revisions and were not rerun; no new Windows, original-module, game/GPU, console or performance result is claimed.

Earlier validation (historical source pins)

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, main ccab14d394, head 783b351490.

  • Differential run: a scratch loader runs a title's decrypted libc.prx natively and compares its swscanf with this build's swscanf_nid_postfix. It checks the result, the value, %n and errno on 60,000 random inputs per format: %d %u %i %x %lld %hd %f %lf %e, with signs, prefixes, digits and large values.
    • On main, %f and %e differ on 557 and 593 inputs, all errno.
    • With this change no format differs, on the copies bundled with PPSA07923, PPSA05916 and PPSA13934.
    • The table rows were checked one by one on PPSA07923's copy, wide and narrow.
  • guest_wide_conversion: new checks cover an overflowing %f and %lf and an underflowing %lf and %e with errno left at 0, and a %Lf of 1e-5000 with ERANGE. The wide, scanning, stream, libc and string tests (33) pass in three runs out of three. With main's ScanningWide.cpp and these checks, guest_wide_conversion fails.
  • python tools/check_conventions.py --base origin/main and git diff --check pass.
  • Overlap: #9863, #8306 and #6879 also edit ScanningWide.cpp, but not the float conversion. #9863 merges cleanly. #8306 conflicts only in docs/dev/debt/libc.md, which this PR does not touch, and it already conflicts with main there. #6879 already conflicts with main in several files.
  • Linux host probe only: Linux 7.2.9, glibc 2.44: narrow sscanf of 1e50 with %f, 1e500 and 1e-500 with %lf, and 1e-4950 with %Lf each assigns one field and sets ERANGE in three repetitions. Normal 1 with %Lf preserves errno sentinel 123. Host strtold("1e-4950") also sets ERANGE. This probe does not execute AnyPS5 or establish console behavior.
  • Linux AnyPS5 CPU tests: base ccab14d39455dd88eb5eda9e926709fe8c799f7b and candidate 783b3514900de7a50342ee0353b81be28bf36d5f each pass all 8 selected CTests in one run: guest_scan_count, guest_wide_formatting, guest_wide_strings, guest_wide_conversion, guest_wide_stream_scan, guest_stdio_variadic, guest_formatting, and guest_strings. The negative control, base production source plus the candidate regression test (f6dbc52d895d21e7b7e62ac462918ed0621d7cf0), passes 7/8 and fails only guest_wide_conversion on the three new overflow/underflow errno checks. Linux test report. These are CPU regression results, with no native title or performance claim.
  • Not tested: a console.

Checklist

  • Based on current main; no other open PR changes the wide float conversion's errno
  • 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: none
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4976435744 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9973). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T01:26:17Z. --- ### What The wide scanf family (`swscanf` and the stream forms that run its scanner) converts `%f`, `%e`, `%g` and `%a` with `strtof`, `strtod` or `strtold`. Those set `ERANGE` when the value overflows or underflows. The `libc.prx` that titles ship stores the same infinity or zero, but sets `ERANGE` only for a `long double`: | Input | Format | Bundled-library `errno` | Windows `main` `errno` | |---|---|---|---| | `1e39`, `1e-50` | `%f`, `%e` | unchanged | `ERANGE` | | `1e400`, `1e-400` | `%lf` | unchanged | `ERANGE` | | `1e5000`, `1e-5000`, `1e4933` | `%Lf`, `%Le` | `ERANGE` | `ERANGE` | | `1e-4950` (a representable `long double`) | `%Lf` | unchanged | unchanged | The narrow `sscanf` of the same copies gives the same `errno` in all these cases. This PR restores `errno` after the `float` and `double` conversions. The `long double` one keeps `strtold`'s `ERANGE`. Values are unchanged. **Remaining gaps.** Narrow `sscanf` delegates floating conversions to host libc on both Linux and Windows. Out-of-range `%f`/`%e` and `%lf` can therefore still leak host `ERANGE`; this PR fixes the wide scanner only. The unchanged `strtold` path also differs between hosts: on Linux/glibc 2.44, `1e-4950` sets `ERANGE`, including in the host narrow `%Lf` scan, whereas the Windows and bundled-library results in the table leave it unchanged. Fixing these gaps requires per-field errno handling while retaining legitimate range errors from other conversions in the same call. ### Tested **Current reviewer-requested Linux refresh (2026-10-25):** exact head `c228d7a459cea0e7dca1f8972915786c14d23d64` directly on main `3a930342d5a2189b78643818f04b8948fc4c3dc9`, two topic commits without merge commits. The entire production scanner is current main plus the original `Float` method and `<cerrno>` include; that method and the original eleven-line regression block are unchanged from previous head `93d553448df31ae614cfd28f9eedc9378fac0648`. Main's canonical `Api.hpp` include, newer `%S`/`%C` matching-failure behavior and all its other scanner/test changes are retained. The debt resolution preserves every current-main row and records the confirmed wide float/double errno contract plus both known remaining gaps in a separate concise `floating scan errno` entry. It keeps main's newer scanner descriptions rather than restoring the older combined paragraph. Fresh Linux x86-64 Release libc and all eight scanner test executables built. The eight scanner groups from the earlier Linux report plus `public_headers` each pass three runs (27 passes). Keeping candidate tests and substituting only exact main production `ScanningWide.cpp` makes `guest_wide_conversion` fail on the three added float/double overflow/underflow errno checks; the other eight groups pass 24/24. Restored candidate gives 27 passes and a byte-identical raw `libc.prx`. Conventions, whitespace and merge-tree checks pass. [Exact-head logs, main-source negative control and complete-file preservation proof](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr9973-headc228d7a4-main3a93-linux-scanner-refresh.tar.gz) (SHA-256 `b3ddb52a9674c73bd32396624e26dca3923b9c92c118f95941b335c1556a76c7`). This directly addresses the request to refresh main while retaining the errno cases and documented gaps. Earlier Windows and module differential results below stay pinned to their measured revisions and were not rerun; no new Windows, original-module, game/GPU, console or performance result is claimed. #### Earlier validation (historical source pins) Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` ccab14d394, head 783b351490. - **Differential run:** a scratch loader runs a title's decrypted `libc.prx` natively and compares its `swscanf` with this build's `swscanf_nid_postfix`. It checks the result, the value, `%n` and `errno` on 60,000 random inputs per format: `%d %u %i %x %lld %hd %f %lf %e`, with signs, prefixes, digits and large values. - On `main`, `%f` and `%e` differ on 557 and 593 inputs, all `errno`. - With this change no format differs, on the copies bundled with PPSA07923, PPSA05916 and PPSA13934. - The table rows were checked one by one on PPSA07923's copy, wide and narrow. - **`guest_wide_conversion`:** new checks cover an overflowing `%f` and `%lf` and an underflowing `%lf` and `%e` with `errno` left at 0, and a `%Lf` of `1e-5000` with `ERANGE`. The wide, scanning, stream, libc and string tests (33) pass in three runs out of three. With `main`'s `ScanningWide.cpp` and these checks, `guest_wide_conversion` fails. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - **Overlap:** #9863, #8306 and #6879 also edit `ScanningWide.cpp`, but not the float conversion. #9863 merges cleanly. #8306 conflicts only in `docs/dev/debt/libc.md`, which this PR does not touch, and it already conflicts with `main` there. #6879 already conflicts with `main` in several files. - **Linux host probe only:** Linux 7.2.9, glibc 2.44: narrow `sscanf` of `1e50` with `%f`, `1e500` and `1e-500` with `%lf`, and `1e-4950` with `%Lf` each assigns one field and sets `ERANGE` in three repetitions. Normal `1` with `%Lf` preserves errno sentinel 123. Host `strtold("1e-4950")` also sets `ERANGE`. This probe does not execute AnyPS5 or establish console behavior. - **Linux AnyPS5 CPU tests:** base `ccab14d39455dd88eb5eda9e926709fe8c799f7b` and candidate `783b3514900de7a50342ee0353b81be28bf36d5f` each pass all 8 selected CTests in one run: `guest_scan_count`, `guest_wide_formatting`, `guest_wide_strings`, `guest_wide_conversion`, `guest_wide_stream_scan`, `guest_stdio_variadic`, `guest_formatting`, and `guest_strings`. The negative control, base production source plus the candidate regression test (`f6dbc52d895d21e7b7e62ac462918ed0621d7cf0`), passes 7/8 and fails only `guest_wide_conversion` on the three new overflow/underflow errno checks. [Linux test report](https://github.com/boykopovar/AnyPS5/pull/9973#issuecomment-6357460258). These are CPU regression results, with no native title or performance claim. - **Not tested:** a console. ### Checklist - [x] Based on current `main`; no other open PR changes the wide float conversion's `errno` - [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: none - [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 AI-assisted (Claude Code).
harisibrar1 commented 2026-10-23 03:16:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-23T03:16:32Z.

Review: COMMENTED


Checked against main 3875f8c8ff and glibc stdlib/strtod_l.c (reading only):

  • The change looks right to me. On main, ScanningWide.cpp passes strtof/strtod's ERANGE straight through to the guest, since __error_nid_postfix returns &errno. Restoring it only in the l and default branches keeps strtold's ERANGE for %Lf, as your table says. It still merges cleanly with main.
  • Could you add a line to docs/dev/debt/libc.md, next to the existing "The scanf family" entries, for the gap you describe under "Not changed"? On main, narrow sscanf goes to the host on both platforms (Formatting.cpp:97 uses glibc's __isoc99_vsscanf, :99 uses std::vsscanf), so an out-of-range %f/%e/%lf there still sets ERANGE. Right now that is only recorded in the PR text.
  • Small point about Linux: I couldn't run this, but reading glibc's round_and_return suggests strtold("1e-4950") (a subnormal long double) sets ERANGE there. If so, the "unchanged" in the 1e-4950 row holds on Windows only, and both %Lf and narrow sscanf would differ from the console on Linux. Worth mentioning in the same debt line if you can confirm it.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5623488058 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9973#pullrequestreview-5623488058). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-23T03:16:32Z. **Review:** COMMENTED --- Checked against main 3875f8c8ff and glibc `stdlib/strtod_l.c` (reading only): - The change looks right to me. On main, `ScanningWide.cpp` passes `strtof`/`strtod`'s `ERANGE` straight through to the guest, since `__error_nid_postfix` returns `&errno`. Restoring it only in the `l` and default branches keeps `strtold`'s `ERANGE` for `%Lf`, as your table says. It still merges cleanly with main. - Could you add a line to `docs/dev/debt/libc.md`, next to the existing "The scanf family" entries, for the gap you describe under "Not changed"? On main, narrow `sscanf` goes to the host on both platforms (`Formatting.cpp:97` uses glibc's `__isoc99_vsscanf`, `:99` uses `std::vsscanf`), so an out-of-range `%f`/`%e`/`%lf` there still sets `ERANGE`. Right now that is only recorded in the PR text. - Small point about Linux: I couldn't run this, but reading glibc's `round_and_return` suggests `strtold("1e-4950")` (a subnormal `long double`) sets `ERANGE` there. If so, the "unchanged" in the `1e-4950` row holds on Windows only, and both `%Lf` and narrow `sscanf` would differ from the console on Linux. Worth mentioning in the same debt line if you can confirm it. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-10-23 04:16:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T04:16:34Z.

Review: CHANGES_REQUESTED


Reviewed exact head 783b3514900de7a50342ee0353b81be28bf36d5f. The conversion change is narrow and consistent with the submitted console differential: restoring the prior errno for %f/%e/%g/%a float and double conversions preserves the converted values while leaving strtold's range result intact. The reported 60,000-input-per-format native comparisons and focused regression cases are strong evidence. Current-main conventions and git diff --check pass; all listed relinker, sanitizer, register-context and write-watch checks pass. Product builds are skipped by policy. I could not rerun guest_wide_conversion locally because this workspace has no configured AnyPS5 build/test executable.

I am requesting one documentation change before approval: the existing The scanf family / swscanf debt entry does not yet describe this errno contract or the remaining gaps listed in the PR (narrow scans still inherit host ERANGE, and glibc's strtold differs for representable subnormal long doubles). Please record the confirmed wide-scan behavior and these known differences in docs/dev/debt/libc.md; the PR description alone will not travel with the implementation.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5623825696 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9973#pullrequestreview-5623825696). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T04:16:34Z. **Review:** CHANGES_REQUESTED --- Reviewed exact head 783b3514900de7a50342ee0353b81be28bf36d5f. The conversion change is narrow and consistent with the submitted console differential: restoring the prior errno for `%f`/`%e`/`%g`/`%a` float and double conversions preserves the converted values while leaving `strtold`'s range result intact. The reported 60,000-input-per-format native comparisons and focused regression cases are strong evidence. Current-main conventions and `git diff --check` pass; all listed relinker, sanitizer, register-context and write-watch checks pass. Product builds are skipped by policy. I could not rerun `guest_wide_conversion` locally because this workspace has no configured AnyPS5 build/test executable. I am requesting one documentation change before approval: the existing `The scanf family` / `swscanf` debt entry does not yet describe this errno contract or the remaining gaps listed in the PR (narrow scans still inherit host `ERANGE`, and glibc's `strtold` differs for representable subnormal long doubles). Please record the confirmed wide-scan behavior and these known differences in `docs/dev/debt/libc.md`; the PR description alone will not travel with the implementation.
MarcZX10 commented 2026-10-23 14:27:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T14:27:37Z.

Review: DISMISSED


I rechecked exact head 907cae23fd4494ee741768582eb20fc77f161110. The documentation follow-up addresses my earlier request, and the reported Windows guest_wide_conversion rerun is on this head. Exact conventions and whitespace checks pass; the PR's focused runtime evidence is still the contributor-reported Windows three-run result and the earlier Linux negative-control run. Hosted product jobs are skipped.

I can't approve the current head yet because it is 423 commits behind live main (e94545a5, merge base 0db47b263d). GuestWideConversion.cpp and docs/dev/debt/libc.md both changed on main after that base. check_main_sync.py reports a clean merge, but CONTRIBUTING requires branches to stay current; please merge/rebase current main, preserve the new errno cases and debt wording, and rerun guest_wide_conversion plus conventions on that exact refreshed head. The implementation file itself has no intervening main changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5629677970 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9973#pullrequestreview-5629677970). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T14:27:37Z. **Review:** DISMISSED --- I rechecked exact head `907cae23fd4494ee741768582eb20fc77f161110`. The documentation follow-up addresses my earlier request, and the reported Windows `guest_wide_conversion` rerun is on this head. Exact conventions and whitespace checks pass; the PR's focused runtime evidence is still the contributor-reported Windows three-run result and the earlier Linux negative-control run. Hosted product jobs are skipped. I can't approve the current head yet because it is 423 commits behind live `main` (`e94545a5`, merge base `0db47b263d`). `GuestWideConversion.cpp` and `docs/dev/debt/libc.md` both changed on main after that base. `check_main_sync.py` reports a clean merge, but CONTRIBUTING requires branches to stay current; please merge/rebase current main, preserve the new errno cases and debt wording, and rerun `guest_wide_conversion` plus conventions on that exact refreshed head. The implementation file itself has no intervening main changes.
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!9973
No description provided.