fix(libSceJson2): saturate the decimal exponent instead of wrapping it #16207

Merged
MarcZX10 merged 4 commits from upstream-pr/16207 into main 2026-11-24 00:43:42 +00:00
MarcZX10 commented 2026-11-05 20:44:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-05T20:44:58Z.


What

  • sce::Json::Parser decimal conversion (ParseDecimal in core/libs/prx/libSceJson2/Export.cpp) accumulated the exponent digits in a std::uint32_t with no overflow check. Exponents past 2^32 wrapped modulo 2^32, so 1e4294967296 scaled by 10^0 and parsed as 1.0 instead of overflowing. The same happened to the negative and underflow forms.
  • Fix (fix(libSceJson2) commit): exponent digits stop accumulating once the exponent reaches 100000. The scaling loop already ends as soon as the magnitude becomes zero or infinite, which takes at most about 700 steps, so every larger exponent gives the same result as an unbounded one.
  • Test (test(libSceJson2) commit): new cases in the existing core/libs/tests/GuestJsonDecimalRange.cpp (already registered as guest_json_decimal_range): 1e4294967296 and -1e4294967296 overflow to infinity, 1e-4294967296 and -1e-4294967296 underflow to zero of their sign, 1e4294967295 overflows, and a long run of leading zeros followed by 4294967296 overflows.
  • Docs (docs(libSceJson2) commit): docs/dev/debt/libSceJson2.md records that the exponent counter width of the reference conversion at 0x7750 is not measured on the console, so the console result for exponents past 2^32 and past 100000 is unverified.
  • Overlap: no other open PR changes ParseDecimal. #12778 adds a different JSON reader test file.

Tested

  • Run on the pushed head: python3 tools/check_conventions.py --base upstream/main (all checks passed), git diff --check upstream/main (clean), python3 core/libs/tests/test_public_headers.py --audit-only (406 libraries, 30604 exports verified; no export declarations changed), and tools/pr_precheck.py (diff check, conventions, export ABI audit and test-quality heuristics passed; its build checks were not run).
  • Not run yet: the build and the guest_json_decimal_range ctest. They are queued and not yet run; this section will be updated with their result.
  • Reasoned, not run: before the fix the exponent of 1e4294967296 wraps to 0, so the parse returns 1.0 and the new cases fail; after the fix they return the infinities and zeros listed above.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5178442388 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16207). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-05T20:44:58Z. --- ### What - `sce::Json::Parser` decimal conversion (`ParseDecimal` in `core/libs/prx/libSceJson2/Export.cpp`) accumulated the exponent digits in a `std::uint32_t` with no overflow check. Exponents past 2^32 wrapped modulo 2^32, so `1e4294967296` scaled by 10^0 and parsed as 1.0 instead of overflowing. The same happened to the negative and underflow forms. - Fix (`fix(libSceJson2)` commit): exponent digits stop accumulating once the exponent reaches 100000. The scaling loop already ends as soon as the magnitude becomes zero or infinite, which takes at most about 700 steps, so every larger exponent gives the same result as an unbounded one. - Test (`test(libSceJson2)` commit): new cases in the existing `core/libs/tests/GuestJsonDecimalRange.cpp` (already registered as `guest_json_decimal_range`): `1e4294967296` and `-1e4294967296` overflow to infinity, `1e-4294967296` and `-1e-4294967296` underflow to zero of their sign, `1e4294967295` overflows, and a long run of leading zeros followed by `4294967296` overflows. - Docs (`docs(libSceJson2)` commit): `docs/dev/debt/libSceJson2.md` records that the exponent counter width of the reference conversion at `0x7750` is not measured on the console, so the console result for exponents past 2^32 and past 100000 is unverified. - Overlap: no other open PR changes `ParseDecimal`. #12778 adds a different JSON reader test file. ### Tested - Run on the pushed head: `python3 tools/check_conventions.py --base upstream/main` (all checks passed), `git diff --check upstream/main` (clean), `python3 core/libs/tests/test_public_headers.py --audit-only` (406 libraries, 30604 exports verified; no export declarations changed), and `tools/pr_precheck.py` (diff check, conventions, export ABI audit and test-quality heuristics passed; its build checks were not run). - Not run yet: the build and the `guest_json_decimal_range` ctest. They are queued and not yet run; this section will be updated with their result. - Reasoned, not run: before the fix the exponent of `1e4294967296` wraps to 0, so the parse returns 1.0 and the new cases fail; after the fix they return the infinities and zeros listed above. ### 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 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
gabichuuuuu commented 2026-11-10 11:12:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-10T11:12:17Z.

Review: COMMENTED


Read 33805b3c5d against main.

Correctness. In ParseDecimal (core/libs/prx/libSceJson2/Export.cpp) the exponent stops accumulating once it reaches 100000 (so it never exceeds 999999 and cannot wrap). The bound is safe: the scaling loop ends when magnitude becomes 0 or infinite, and from any nonzero finite double that takes well under 700 multiplications or divisions by 10, so every exponent above 100000 gives the same result an unbounded counter would. Leading zeros keep the counter at 0, so 1e000...04294967296 still reaches the large value.

Test. The new GuestJsonDecimalRange.cpp cases fail on main: std::uint32_t exponent wraps 4294967296 to 0, so 1e4294967296 parses as 1.0, not infinity. The Tested section still says the build and guest_json_decimal_range were not run; a Windows run of this head is reported in the PR comments, so the section should be updated with that result or one of your own.

Non-blocking. The existing JSON real-number debt entry says decimal conversion follows the instructions at 0x7750 and keeps the reference's results even where they differ from standard conversion. If that routine uses a 32-bit counter, main's wrap is the reference result and this change moves away from it. The new debt entry is honest that the counter width is unmeasured; reading the exponent loop at 0x7750 would settle which behaviour is right.

Not run here; this is from reading the code.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5840904444 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16207#pullrequestreview-5840904444). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-10T11:12:17Z. **Review:** COMMENTED --- Read `33805b3c5d` against `main`. **Correctness.** In `ParseDecimal` (`core/libs/prx/libSceJson2/Export.cpp`) the exponent stops accumulating once it reaches 100000 (so it never exceeds 999999 and cannot wrap). The bound is safe: the scaling loop ends when `magnitude` becomes 0 or infinite, and from any nonzero finite double that takes well under 700 multiplications or divisions by 10, so every exponent above 100000 gives the same result an unbounded counter would. Leading zeros keep the counter at 0, so `1e000...04294967296` still reaches the large value. **Test.** The new `GuestJsonDecimalRange.cpp` cases fail on `main`: `std::uint32_t exponent` wraps `4294967296` to 0, so `1e4294967296` parses as 1.0, not infinity. The `Tested` section still says the build and `guest_json_decimal_range` were not run; a Windows run of this head is reported in the PR comments, so the section should be updated with that result or one of your own. **Non-blocking.** The existing JSON real-number debt entry says decimal conversion follows the instructions at `0x7750` and keeps the reference's results even where they differ from standard conversion. If that routine uses a 32-bit counter, `main`'s wrap is the reference result and this change moves away from it. The new debt entry is honest that the counter width is unmeasured; reading the exponent loop at `0x7750` would settle which behaviour is right. Not run here; this is from reading the code. AI-assisted: yes.
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!16207
No description provided.