fix(libSceNpCommon): avoid hexadecimal capacity overflow #12772

Merged
StoneDC merged 1 commit from upstream-pr/12772 into main 2026-11-05 11:05:54 +00:00
StoneDC commented 2026-10-29 08:40:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by StoneDC on 2026-10-29T08:40:50Z.


What

Prevents unsigned overflow in sceNpStrBuildHex capacity validation. The old length * 2 expression can wrap and admit an undersized output; the new size == 0 / (size - 1) / 2 guard checks the same intended capacity condition before dereferencing input or writing output.

Implementation basis and limits: the existing capacity-first rule and NP_ERROR_BUFFER_TOO_SMALL in libSceNpCommon/Strings.cpp, plus the mathematical requirement 2*length+1 <= size. This retains the current error code rather than inventing firmware error priority. Ordinary sizes, case selection, NUL termination and no-write-on-short-output behavior are covered. PS5 behavior for impossible lengths was not measured.

Integration review: the exact topic head was checked with the repository overlap tool against main edf6804cf52c62eacafc592dd6142cb4efe5bd05. No overlapping open PR was reported. All 20 topic commits also merged independently into current validation main 4c7d275bef89d309313d16bc2bc05d37a1b50be2; their combined integration 2a0489dd307a9c98b9b353ea7282b65809db07dd was built and tested. Depends on: none; no other candidate branch is required to build or exercise this change.

Tested

The same new fixture against the exact old production Strings.cpp triggered AddressSanitizer stack-buffer-overflow; the changed production source passed ASan+UBSan. Full-configuration Linux and Windows builds ran guest_np_hex_capacity against the actual library, including huge lengths and ordinary capacity boundaries; no skips.

Topic head: a0ee7cda10450c6b091a9806a4d999dbc6b5b7da, based on f88c86bf6cdd2c74bcdb31e45ba877d4993e62e5. Local platforms: Ubuntu/WSL GCC 15.2, CMake 4.2.3, Python 3.14.4; Windows MinGW GCC 15.2, CMake 4.3.1, Python 3.14.3. Applicable conventions, whitespace, script registration and current-main three-way merge checks passed. Linux full-product builds and subsequent focused library rebuilds were performed; Windows used the full configuration and the affected library/test targets. The combined 11 system-library CTests passed on both platforms. No game or PS5 measurement is claimed; macOS and hosted full-build CI are not reported as passed.

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:5064934058 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12772). Originally posted by [StoneDC](https://github.com/StoneDC) on 2026-10-29T08:40:50Z. --- ### What Prevents unsigned overflow in sceNpStrBuildHex capacity validation. The old length * 2 expression can wrap and admit an undersized output; the new size == 0 / (size - 1) / 2 guard checks the same intended capacity condition before dereferencing input or writing output. Implementation basis and limits: the existing capacity-first rule and NP_ERROR_BUFFER_TOO_SMALL in libSceNpCommon/Strings.cpp, plus the mathematical requirement 2*length+1 <= size. This retains the current error code rather than inventing firmware error priority. Ordinary sizes, case selection, NUL termination and no-write-on-short-output behavior are covered. PS5 behavior for impossible lengths was not measured. Integration review: the exact topic head was checked with the repository overlap tool against main `edf6804cf52c62eacafc592dd6142cb4efe5bd05`. No overlapping open PR was reported. All 20 topic commits also merged independently into current validation main `4c7d275bef89d309313d16bc2bc05d37a1b50be2`; their combined integration `2a0489dd307a9c98b9b353ea7282b65809db07dd` was built and tested. Depends on: none; no other candidate branch is required to build or exercise this change. ### Tested <!-- 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. --> The same new fixture against the exact old production Strings.cpp triggered AddressSanitizer stack-buffer-overflow; the changed production source passed ASan+UBSan. Full-configuration Linux and Windows builds ran guest_np_hex_capacity against the actual library, including huge lengths and ordinary capacity boundaries; no skips. Topic head: `a0ee7cda10450c6b091a9806a4d999dbc6b5b7da`, based on `f88c86bf6cdd2c74bcdb31e45ba877d4993e62e5`. Local platforms: Ubuntu/WSL GCC 15.2, CMake 4.2.3, Python 3.14.4; Windows MinGW GCC 15.2, CMake 4.3.1, Python 3.14.3. Applicable conventions, whitespace, script registration and current-main three-way merge checks passed. Linux full-product builds and subsequent focused library rebuilds were performed; Windows used the full configuration and the affected library/test targets. The combined 11 system-library CTests passed on both platforms. No game or PS5 measurement is claimed; macOS and hosted full-build CI are not reported as passed. ### 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: <!-- #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
BunnyFl commented 2026-11-02 01:42:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T01:42:36Z.

Review: APPROVED


Windows 11, WinLibs GCC 15.2.0 r7, Release. Head a0ee7cda10 merged into upstream/main 93dbd0f82a:

  • guest_np_hex_capacity passes.
  • Negative control: the same test with main's Strings.cpp segfaults (Exception: SegFault). That's the wrapped length * 2 admitting the huge lengths and writing past the 16-byte buffer, the same overflow you saw under ASan.

size == 0 || length > (size - 1) / 2 is exactly size < 2 * length + 1 without the multiplication, so the ordinary boundaries are unchanged. The capacity loop checks that.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5731296947 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12772#pullrequestreview-5731296947). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T01:42:36Z. **Review:** APPROVED --- Windows 11, WinLibs GCC 15.2.0 r7, Release. Head a0ee7cda10 merged into upstream/main 93dbd0f82a: - `guest_np_hex_capacity` passes. - Negative control: the same test with main's `Strings.cpp` segfaults (`Exception: SegFault`). That's the wrapped `length * 2` admitting the huge lengths and writing past the 16-byte buffer, the same overflow you saw under ASan. `size == 0 || length > (size - 1) / 2` is exactly `size < 2 * length + 1` without the multiplication, so the ordinary boundaries are unchanged. The capacity loop checks that.
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!12772
No description provided.