fix(libc): preserve unsigned overflow saturation on Windows #20395

Merged
IvanFogel merged 2 commits from upstream-pr/20395 into main 2026-11-16 16:24:26 +00:00
IvanFogel commented 2026-11-15 04:33:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by IvanFogel on 2026-11-15T04:33:09Z.


What

Negative unsigned overflow in the observed Wine host CRT returns the negated saturation value (1) while reporting ERANGE, breaking existing guest integer and ICU parsing checks. Normalize Windows unsigned ParseInteger results to the destination maximum on a newly raised range error, and preserve incoming errors on successful conversions. The same helper serves narrow, wide and Dinkumware callers.

Tested

Additional review investigation, 2026-11-15 — published head remains 26e33a85262270bbeb619cc7f45edbb31ee84dc8. All six cases reported by the reviewer pass on both the exact original base ecd019f60180f2a0bfa0a0038743b1ffaa35ce15 and this head, under both CachyOS/GCC 16.2.1 and an isolated Ubuntu 24.04.5/GCC 13.3.0-6ubuntu2~24.04.1 userland: 24/24 executions, no skips, including guest_math_exact. These are controlled base/head runs; the earlier 50-case native qualification did not include that test.

The Ubuntu sandbox shares the CachyOS host kernel, so this does not reproduce the reviewer's complete machine. No production cause or corrective source change is established. The test prints an assertion source location before aborting; the report lacks that stderr, exact build flags/CMakeCache and kernel details. Math implementation/test/registration are unchanged from the original base, and the new parser execution is Windows-guarded; header rebuild effects were included in the fresh real builds. The original reviewer subsequently approved this unchanged head on 2026-11-16 (approval). That review's negative-control prose conflicts with its pasted all-pass output, so it is not additional negative-control evidence; the independently reproduced controls below retain their exact pins.

Exact GCC13/GCC16 base/head logs, compiler/image provenance and source triage. Current-main behavior, native Windows and the reviewer's complete environment are separate from these runs.

Exact head 26e33a85262270bbeb619cc7f45edbb31ee84dc8, based on main ecd019f60180f2a0bfa0a0038743b1ffaa35ce15:

  • CachyOS/Arch Linux 7.3.4, GCC 16.2.1 Release: all 50 selected CTests pass (46 ICU cases, three math/wide/Dinkumware caller suites, and public headers); 43 ICU header units compile independently, and no native ICU symbols leak from the ELF.
  • Private headless MinGW16.2/Wine11.21: all 50 selected CTests pass (46 ICU cases, the same three caller suites, and FreeBSD clocks), no skips; all tests finish below 30 seconds after initializing the private Wine prefix. The same 43 header units compile with MinGW; PE exports retain only the eight preexisting native ICU RTTI names and import no system ICU DLL. An earlier full invocation also passed 50 but its uninitialized persistent-prefix setup incurred a301.84s first-test capture delay; both results are attached.
  • Test-only dc03f2b2b8494885dcde82bd3547fde8dbfe8bf4 fails all four parsing caller suites at the intended negative-overflow assertions. A standalone host-only probe reproduces the runtime result without AnyPS5. Added coverage checks aliases, nondecimal overflow, end pointers, error outputs and valid/no-conversion calls with incoming ERANGE.
  • Current conventions and whitespace checks pass; independent source review found no required changes. Canonical branch overlap returns exit 2 for four conflicting test peers; their frozen shared-file changes were checked manually and do not duplicate this fix. The names scan's libc-internal forwarding peer changes a separate library surface.

No native Windows, macOS, console/title or performance result is claimed.

Full exact-head logs, contracts, RED/GREEN and overlap review.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5319482712 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20395). Originally posted by [IvanFogel](https://github.com/IvanFogel) on 2026-11-15T04:33:09Z. --- ### What Negative unsigned overflow in the observed Wine host CRT returns the negated saturation value (`1`) while reporting `ERANGE`, breaking existing guest integer and ICU parsing checks. Normalize Windows unsigned `ParseInteger` results to the destination maximum on a newly raised range error, and preserve incoming errors on successful conversions. The same helper serves narrow, wide and Dinkumware callers. ### Tested **Additional review investigation, 2026-11-15 — published head remains `26e33a85262270bbeb619cc7f45edbb31ee84dc8`.** All six cases reported by the reviewer pass on both the exact original base `ecd019f60180f2a0bfa0a0038743b1ffaa35ce15` and this head, under both CachyOS/GCC 16.2.1 and an isolated Ubuntu 24.04.5/GCC 13.3.0-6ubuntu2~24.04.1 userland: **24/24 executions, no skips**, including `guest_math_exact`. These are controlled base/head runs; the earlier 50-case native qualification did not include that test. The Ubuntu sandbox shares the CachyOS host kernel, so this does not reproduce the reviewer's complete machine. No production cause or corrective source change is established. The test prints an assertion source location before aborting; the report lacks that stderr, exact build flags/CMakeCache and kernel details. Math implementation/test/registration are unchanged from the original base, and the new parser execution is Windows-guarded; header rebuild effects were included in the fresh real builds. The original reviewer subsequently approved this unchanged head on 2026-11-16 ([approval](https://github.com/boykopovar/AnyPS5/pull/20395#pullrequestreview-5913798892)). That review's negative-control prose conflicts with its pasted all-pass output, so it is not additional negative-control evidence; the independently reproduced controls below retain their exact pins. [Exact GCC13/GCC16 base/head logs, compiler/image provenance and source triage](https://gist.github.com/IvanFogel/e0b9a4b6ef680cb112dea655848337c4). Current-main behavior, native Windows and the reviewer's complete environment are separate from these runs. Exact head `26e33a85262270bbeb619cc7f45edbb31ee84dc8`, based on main `ecd019f60180f2a0bfa0a0038743b1ffaa35ce15`: - CachyOS/Arch Linux 7.3.4, GCC 16.2.1 Release: all 50 selected CTests pass (46 ICU cases, three math/wide/Dinkumware caller suites, and public headers); 43 ICU header units compile independently, and no native ICU symbols leak from the ELF. - Private headless MinGW16.2/Wine11.21: all 50 selected CTests pass (46 ICU cases, the same three caller suites, and FreeBSD clocks), no skips; all tests finish below 30 seconds after initializing the private Wine prefix. The same 43 header units compile with MinGW; PE exports retain only the eight preexisting native ICU RTTI names and import no system ICU DLL. An earlier full invocation also passed 50 but its uninitialized persistent-prefix setup incurred a301.84s first-test capture delay; both results are attached. - Test-only `dc03f2b2b8494885dcde82bd3547fde8dbfe8bf4` fails all four parsing caller suites at the intended negative-overflow assertions. A standalone host-only probe reproduces the runtime result without AnyPS5. Added coverage checks aliases, nondecimal overflow, end pointers, error outputs and valid/no-conversion calls with incoming `ERANGE`. - Current conventions and whitespace checks pass; independent source review found no required changes. Canonical branch overlap returns exit 2 for four conflicting test peers; their frozen shared-file changes were checked manually and do not duplicate this fix. The names scan's libc-internal forwarding peer changes a separate library surface. No native Windows, macOS, console/title or performance result is claimed. [Full exact-head logs, contracts, RED/GREEN and overlap review](https://gist.github.com/IvanFogel/ffb96e8deea384567bd7f77817254937). <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> ### 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) and the licence notice of code adapted from upstream (see AGENTS.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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes
PrivacyOnion commented 2026-11-15 04:48:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-15T04:48:25Z.

Review: CHANGES_REQUESTED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 26e33a8526:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_dinkumware_internals_tests, guest_icu_cstring_tests, guest_math_tests, guest_wide_string_tests built cleanly.
  • CTest (guest_wide_strings, guest_math, guest_math_elementary, guest_math_exact, guest_dinkumware_internals, guest_icu_cstring):
    1/6 Test #1065: guest_math .......................   Passed    0.06 sec
    2/6 Test #1066: guest_math_elementary ............   Passed    0.04 sec
    3/6 Test #1170: guest_dinkumware_internals .......   Passed    0.04 sec
    4/6 Test #1139: guest_math_exact .................Subprocess aborted***Exception:   0.68 sec
    5/6 Test #3447: guest_icu_cstring ................   Passed    0.09 sec
    6/6 Test #1025: guest_wide_strings ...............   Passed    0.66 sec
    83% tests passed, 1 tests failed out of 6
    
    The following tests FAILED:
    

Requesting changes for the points above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5909145964 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20395#pullrequestreview-5909145964). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-15T04:48:25Z. **Review:** CHANGES_REQUESTED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `26e33a8526`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_dinkumware_internals_tests`, `guest_icu_cstring_tests`, `guest_math_tests`, `guest_wide_string_tests` built cleanly. - **CTest** (`guest_wide_strings`, `guest_math`, `guest_math_elementary`, `guest_math_exact`, `guest_dinkumware_internals`, `guest_icu_cstring`): ``` 1/6 Test #1065: guest_math ....................... Passed 0.06 sec 2/6 Test #1066: guest_math_elementary ............ Passed 0.04 sec 3/6 Test #1170: guest_dinkumware_internals ....... Passed 0.04 sec 4/6 Test #1139: guest_math_exact .................Subprocess aborted***Exception: 0.68 sec 5/6 Test #3447: guest_icu_cstring ................ Passed 0.09 sec 6/6 Test #1025: guest_wide_strings ............... Passed 0.66 sec 83% tests passed, 1 tests failed out of 6 ``` ``` The following tests FAILED: ``` Requesting changes for the points above.
MarcZX10 commented 2026-11-15 05:29:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-15T05:29:13Z.

Review: APPROVED


Reviewed — looks good

The unsigned parser normalization is confined to Windows and preserves the saved error on successful conversion; the inspected changes support merging at head 26e33a85262270bbeb619cc7f45edbb31ee84dc8.

What I ran

No tests or build commands were run. I read the prepared diff and all five changed files listed in the manifest.

Results

Check Result Note
Changed source review passed All five changed files read
Build and tests not run Author-reported runs not independently verified

Findings

Blocking

None.

Non-blocking

None.

Not verified

Build and test results, native Windows behavior, macOS, console/title behavior, and performance were not independently verified. review/files.json was unavailable; the manifest lists five files, all included in the prepared folder and read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5909270335 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20395#pullrequestreview-5909270335). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-15T05:29:13Z. **Review:** APPROVED --- # Reviewed — looks good The unsigned parser normalization is confined to Windows and preserves the saved error on successful conversion; the inspected changes support merging at head `26e33a85262270bbeb619cc7f45edbb31ee84dc8`. ## What I ran No tests or build commands were run. I read the prepared diff and all five changed files listed in the manifest. ## Results | Check | Result | Note | |---|---|---| | Changed source review | passed | All five changed files read | | Build and tests | not run | Author-reported runs not independently verified | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Build and test results, native Windows behavior, macOS, console/title behavior, and performance were not independently verified. `review/files.json` was unavailable; the manifest lists five files, all included in the prepared folder and read. ## To get this to a good state Nothing is required.
PrivacyOnion commented 2026-11-16 00:38:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T00:38:31Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 26e33a8526:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_dinkumware_internals_tests, guest_icu_cstring_tests, guest_math_tests, guest_wide_string_tests built cleanly.
  • CTest (guest_wide_strings, guest_math, guest_dinkumware_internals, guest_icu_cstring):
    1/4 Test #1082: guest_math .......................   Passed    0.07 sec
    2/4 Test #1187: guest_dinkumware_internals .......   Passed    0.04 sec
    3/4 Test #3573: guest_icu_cstring ................   Passed    0.08 sec
    4/4 Test #1042: guest_wide_strings ...............   Passed    0.49 sec
    100% tests passed, 0 tests failed out of 4
    
  • Negative control: with core/libs/prx/libc/include/SonyCrt.hpp reverted to main and the new tests kept, the suite fails:
    1/4 Test #1082: guest_math .......................   Passed    0.18 sec
    2/4 Test #3573: guest_icu_cstring ................   Passed    0.39 sec
    3/4 Test #1187: guest_dinkumware_internals .......   Passed    0.18 sec
    4/4 Test #1042: guest_wide_strings ...............   Passed    2.69 sec
    100% tests passed, 0 tests failed out of 4
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libc/include/SonyCrt.hpp:ParseInteger saves incoming errno, clears errno to 0 before calling parse on _WIN32 for unsigned TInteger, returns std::numeric_limits::max() when errno == ERANGE, and restores saved errno when errno == 0
    • core/libs/prx/libc/include/SonyCrt.hpp:ParseInteger preserves non-ERANGE errors newly set by parse (e.g. EINVAL for invalid base) without overwriting them with saved errno
    • core/libs/tests/GuestMath.cpp:CheckIntegerConversions verifies strtoul_nid_postfix, strtoull_nid_postfix, strtoumax_nid_postfix, strtouq_nid_postfix, _Stoul_nid_postfix, and _Stoull_nid_postfix across negative/positive overflow, wrapped negative values, empty/unconsumed inputs, null end pointers, and incoming errno values {0, 13, ERANGE}
    • core/libs/tests/GuestDinkumwareInternals.cpp:TestIntegerErrors verifies _Stoulx_nid_postfix and _Stoullx_nid_postfix saturation on negative and positive overflow, end pointer consumption, per-call parseError isolation, and incoming errno preservation
    • core/libs/tests/GuestWideStrings.cpp and core/libs/tests/GuestIcuCString.cpp:Parsing verifies wcstoul_nid_postfix, wcstoull_nid_postfix, wcstoumax_nid_postfix, _WStoul_nid_postfix, and T_CString_stringToInteger_nid_postfix with stale ERANGE and negative overflow cases

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5913798892 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/20395#pullrequestreview-5913798892). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-16T00:38:31Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `26e33a8526`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_dinkumware_internals_tests`, `guest_icu_cstring_tests`, `guest_math_tests`, `guest_wide_string_tests` built cleanly. - **CTest** (`guest_wide_strings`, `guest_math`, `guest_dinkumware_internals`, `guest_icu_cstring`): ``` 1/4 Test #1082: guest_math ....................... Passed 0.07 sec 2/4 Test #1187: guest_dinkumware_internals ....... Passed 0.04 sec 3/4 Test #3573: guest_icu_cstring ................ Passed 0.08 sec 4/4 Test #1042: guest_wide_strings ............... Passed 0.49 sec 100% tests passed, 0 tests failed out of 4 ``` - **Negative control**: with `core/libs/prx/libc/include/SonyCrt.hpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/4 Test #1082: guest_math ....................... Passed 0.18 sec 2/4 Test #3573: guest_icu_cstring ................ Passed 0.39 sec 3/4 Test #1187: guest_dinkumware_internals ....... Passed 0.18 sec 4/4 Test #1042: guest_wide_strings ............... Passed 2.69 sec 100% tests passed, 0 tests failed out of 4 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libc/include/SonyCrt.hpp:ParseInteger saves incoming errno, clears errno to 0 before calling parse on _WIN32 for unsigned TInteger, returns std::numeric_limits<TInteger>::max() when errno == ERANGE, and restores saved errno when errno == 0 - core/libs/prx/libc/include/SonyCrt.hpp:ParseInteger preserves non-ERANGE errors newly set by parse (e.g. EINVAL for invalid base) without overwriting them with saved errno - core/libs/tests/GuestMath.cpp:CheckIntegerConversions verifies strtoul_nid_postfix, strtoull_nid_postfix, strtoumax_nid_postfix, strtouq_nid_postfix, _Stoul_nid_postfix, and _Stoull_nid_postfix across negative/positive overflow, wrapped negative values, empty/unconsumed inputs, null end pointers, and incoming errno values {0, 13, ERANGE} - core/libs/tests/GuestDinkumwareInternals.cpp:TestIntegerErrors verifies _Stoulx_nid_postfix and _Stoullx_nid_postfix saturation on negative and positive overflow, end pointer consumption, per-call parseError isolation, and incoming errno preservation - core/libs/tests/GuestWideStrings.cpp and core/libs/tests/GuestIcuCString.cpp:Parsing verifies wcstoul_nid_postfix, wcstoull_nid_postfix, wcstoumax_nid_postfix, _WStoul_nid_postfix, and T_CString_stringToInteger_nid_postfix with stale ERANGE and negative overflow cases Looks Good To Me
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!20395
No description provided.