fix(libSceIcu): follow ICU for null escape inputs and negative u_unescape capacities #12451

Merged
simnJS merged 1 commit from upstream-pr/12451 into main 2026-10-29 10:24:09 +00:00
simnJS commented 2026-10-28 14:09:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by simnJS on 2026-10-28T14:09:40Z.


What

u_unescapeAt and u_unescape follow ICU 78.1's ustring.cpp, but throw NotImplemented_nid_no_patch for null pointers and for a negative capacity with a destination, so tools/progress.py counts both as stubs. ICU answers or faults in each of these cases:

  • u_unescapeAt reads *offset first and calls charAt once the offset is in range, checking neither. A null offset, or a null callback with an in-range offset, now throws std::invalid_argument (APS5_INVALID_ARG_EX), as #11111 did where LibreSSL faults. An out-of-range offset still returns -1 without calling the callback.
  • u_unescape reads *src without a check, so a null source now throws std::invalid_argument.
  • For a negative capacity with a destination, _appendUChars clamps the capacity to 0, and U16_LENGTH(c32) <= (destCapacity - i), i < destCapacity and destCapacity > 0 are all false. Nothing is written and the full length is returned, as for a capacity of 0. The existing code already gives that answer once the throw is gone.
  • ICU computes destCapacity - i in int32_t at each escape and before each escape-free segment it appends. For a capacity close to INT32_MIN that subtraction overflows, which is undefined. That case still throws NotImplemented_nid_no_patch, at the points where ICU computes it.

A source longer than INT32_MAX bytes also still throws: ICU truncates the remaining length it passes to u_unescapeAt, and its output count overflows. So u_unescape stays counted as to do. The debt entry now says which cases throw which exception.

tools/progress.py --module libSceIcu: 11 → 12 implemented (u_unescapeAt), 109 → 108 to do.

Tested

Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), Release, BUILD_TESTING=ON. Written and tested on main 750cf6cf48, then rebased onto d4cb89300f, which changes none of these files; the tests below and check_conventions were rebuilt and rerun on the rebased head, while the control and pr_overlap ran before the rebase.

  • guest_icu_unescape:

    • The null callback and null offset checks of u_unescapeAt now require std::invalid_argument instead of any exception, and both null at once are covered.
    • A null source, with and without a destination, requires std::invalid_argument and leaves the destination untouched.
    • Negative capacities (-1, -2, INT32_MIN + 1, INT32_MIN) with escapes, plain text, a failing escape and an empty string return the full length (0 for the failing escape). The destination and the units around it stay untouched.
    • Where ICU's subtraction overflows, the call throws and writes nothing, while a null destination still returns the length. The cases are "a\\n", "\\na" and "\\nab" with INT32_MIN, and "ab\\n" with INT32_MIN + 1. One unit more of capacity returns the length ("ab\\n" with INT32_MIN + 2, "\\nab" with INT32_MIN + 1).

    Pass, 10 runs (ctest --repeat until-fail:10).

  • Control: with main's Unescape.cpp and this test, guest_icu_unescape fails with u_unescapeAt: null callback not implemented (exit 0xc0000409).

  • guest_icu_cstring and guest_icu_version: pass.

  • public_headers and public_headers_compile_*: 65/65 pass.

  • python tools/check_conventions.py --base origin/main with this title and body: all checks pass. python tools/pr_overlap.py --branch fix/icu-unescape-invalid-input finds no overlap. It could not compare docs/dev/debt/libSceIcu.md with #11088, #11115, #11118, #11122, #11132, #11136 and #12320, which already conflict with main there. git merge-tree of this head with each of them keeps the Escape decoding entry outside every conflict block. #10962 to #10967 merge cleanly.

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:5051916135 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12451). Originally posted by [simnJS](https://github.com/simnJS) on 2026-10-28T14:09:40Z. --- ### What `u_unescapeAt` and `u_unescape` follow ICU 78.1's `ustring.cpp`, but throw `NotImplemented_nid_no_patch` for null pointers and for a negative capacity with a destination, so `tools/progress.py` counts both as stubs. ICU answers or faults in each of these cases: - [`u_unescapeAt`](https://github.com/unicode-org/icu/blob/049e0d6a420629ac7db77256987d083a563287b5/icu4c/source/common/ustring.cpp#L1212-L1234) reads `*offset` first and calls `charAt` once the offset is in range, checking neither. A null offset, or a null callback with an in-range offset, now throws `std::invalid_argument` (`APS5_INVALID_ARG_EX`), as #11111 did where LibreSSL faults. An out-of-range offset still returns -1 without calling the callback. - [`u_unescape`](https://github.com/unicode-org/icu/blob/049e0d6a420629ac7db77256987d083a563287b5/icu4c/source/common/ustring.cpp#L1376-L1429) reads `*src` without a check, so a null source now throws `std::invalid_argument`. - For a negative capacity with a destination, [`_appendUChars`](https://github.com/unicode-org/icu/blob/049e0d6a420629ac7db77256987d083a563287b5/icu4c/source/common/ustring.cpp#L1364-L1373) clamps the capacity to 0, and `U16_LENGTH(c32) <= (destCapacity - i)`, `i < destCapacity` and `destCapacity > 0` are all false. Nothing is written and the full length is returned, as for a capacity of 0. The existing code already gives that answer once the throw is gone. - ICU computes `destCapacity - i` in `int32_t` at each escape and before each escape-free segment it appends. For a capacity close to `INT32_MIN` that subtraction overflows, which is undefined. That case still throws `NotImplemented_nid_no_patch`, at the points where ICU computes it. A source longer than `INT32_MAX` bytes also still throws: ICU truncates the remaining length it passes to `u_unescapeAt`, and its output count overflows. So `u_unescape` stays counted as to do. The debt entry now says which cases throw which exception. `tools/progress.py --module libSceIcu`: 11 → 12 implemented (`u_unescapeAt`), 109 → 108 to do. ### Tested Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), Release, `BUILD_TESTING=ON`. Written and tested on `main` `750cf6cf48`, then rebased onto `d4cb89300f`, which changes none of these files; the tests below and `check_conventions` were rebuilt and rerun on the rebased head, while the control and `pr_overlap` ran before the rebase. - `guest_icu_unescape`: - The null callback and null offset checks of `u_unescapeAt` now require `std::invalid_argument` instead of any exception, and both null at once are covered. - A null source, with and without a destination, requires `std::invalid_argument` and leaves the destination untouched. - Negative capacities (-1, -2, `INT32_MIN + 1`, `INT32_MIN`) with escapes, plain text, a failing escape and an empty string return the full length (0 for the failing escape). The destination and the units around it stay untouched. - Where ICU's subtraction overflows, the call throws and writes nothing, while a null destination still returns the length. The cases are `"a\\n"`, `"\\na"` and `"\\nab"` with `INT32_MIN`, and `"ab\\n"` with `INT32_MIN + 1`. One unit more of capacity returns the length (`"ab\\n"` with `INT32_MIN + 2`, `"\\nab"` with `INT32_MIN + 1`). Pass, 10 runs (`ctest --repeat until-fail:10`). - Control: with `main`'s `Unescape.cpp` and this test, `guest_icu_unescape` fails with `u_unescapeAt: null callback not implemented` (exit `0xc0000409`). - `guest_icu_cstring` and `guest_icu_version`: pass. - `public_headers` and `public_headers_compile_*`: 65/65 pass. - `python tools/check_conventions.py --base origin/main` with this title and body: all checks pass. `python tools/pr_overlap.py --branch fix/icu-unescape-invalid-input` finds no overlap. It could not compare `docs/dev/debt/libSceIcu.md` with #11088, #11115, #11118, #11122, #11132, #11136 and #12320, which already conflict with `main` there. `git merge-tree` of this head with each of them keeps the Escape decoding entry outside every conflict block. #10962 to #10967 merge cleanly. ### 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
MarcZX10 commented 2026-10-28 14:26:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T14:26:11Z.

Review: APPROVED


Reviewed exact head 07883249e6. The ICU behavior claims match the cited implementation: null pointers that ICU dereferences now use the project invalid-argument exception policy, negative destination capacity writes nothing, and cases where ICU’s signed capacity subtraction would overflow remain explicit throws. The tests cover null precedence, unchanged destination bytes, and boundary cases around INT32_MIN. Hosted conventions, intake, eligibility, report, labels and applicable Windows validation pass; platform-specific jobs outside this change are skipped. I have not independently run the Windows test suite, and no console ABI result is claimed. The current-main merge is clean and git diff --check passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5684255989 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12451#pullrequestreview-5684255989). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-28T14:26:11Z. **Review:** APPROVED --- Reviewed exact head 07883249e6dd1d743fa94819893dc8745d351f07. The ICU behavior claims match the cited implementation: null pointers that ICU dereferences now use the project invalid-argument exception policy, negative destination capacity writes nothing, and cases where ICU’s signed capacity subtraction would overflow remain explicit throws. The tests cover null precedence, unchanged destination bytes, and boundary cases around INT32_MIN. Hosted conventions, intake, eligibility, report, labels and applicable Windows validation pass; platform-specific jobs outside this change are skipped. I have not independently run the Windows test suite, and no console ABI result is claimed. The current-main merge is clean and `git diff --check` passes.
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!12451
No description provided.