fix(libSceIcu): read resource 0 as the empty string in the UTF-8 resource accessors #25371

Merged
kessler-drift merged 1 commit from upstream-pr/25371 into main 2026-11-24 20:08:13 +00:00
kessler-drift commented 2026-11-22 00:15:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by kessler-drift on 2026-11-22T00:15:51Z.


What

ures_getUTF8String, ures_getUTF8StringByIndex and ures_getUTF8StringByKey read every type-0 resource at pRoot + offset * 4, including the resource word 0. ICU stores an empty string as that word, and res_getString maps it to a static empty string instead of reading the bundle (ICU 68.2 uresdata.cpp l.335-338: res==0 ? &gEmptyString.length : pResData->pRoot+res; ICU 78.1 has the same lines). genrb 68.2 writes b { "" } as resource 0 from format version 2 on.

The accessors took pRoot[0], the root resource of the bundle, as the string length: with a root table such as 0x20000040 they tried to copy 2^29 UTF-16 units past the end of the bundle and threw NotImplemented, and with a small pRoot[0] they returned bytes of the bundle. They now return the empty string for resource 0, as ICU does, through the existing empty-string path ("" without forceCopy, a terminated dest with it). The debt entry of these accessors does not mention this case and is unchanged.

Tested

Ubuntu 26.04 x86-64 container under Rosetta on macOS 27.2, GCC 15.2, main 15826135019. A harness compiles one resource file with genrb 68.2 in format versions 1 and 2, opens it with ICU 68.2 (ures_openDirect), copies the 200 bytes of each real UResourceBundle (the root, every item, nested items), and calls the three accessors on each object with ICU 68.2 and with this module (same bytes, pRoot pointed at the module's copy of the same .res file): every index or key, pLength null, capacities -1 to 4, 13, 37, 38, 109 and 150, forceCopy 0 and 1, incoming status 0, -124, -128 and 1, each call in a forked process.

Calls compared with ICU 68.2 main this PR
18 900 calls, results that changed 106
the 106 calls that read resource 0 (format 2), equal to ICU 68.2 0 (throw) 88

The 18 remaining calls differ only by an incoming U_STRING_NOT_TERMINATED_WARNING that ICU clears when it terminates the output, which these accessors do not do for any string; that is a separate change. No other result changed. Two more resource files, generated at random from seeds 20261121 and 20261122 (36 288 and 18 900 calls), give the same picture: 583 and 318 results change, 484 and 264 of them now equal ICU 68.2, the others differ only by that incoming warning, and no other result changes.

guest_icu_resource_utf8 now puts a one-unit string at pRoot[0] and reads resource 0 through all three accessors (a bundle whose resource is 0, with and without forceCopy, and a table whose item is 0, by index and by key); it fails without the change. The 87 libSceIcu tests and the 91 public_headers, registration and progress tests pass, apart from the failures main already has under Rosetta.

Not tested on a console, on Windows or on macOS.

python3 tools/check_conventions.py --base upstream/main passes. python3 tools/pr_overlap.py --names for the three accessors lists no open pull request that changes ResourceUtf8.cpp; #24322, #24910 and #24917 split libraries, #14443, #19670 and #24288 change other resource files.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5433915980 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25371). Originally posted by [kessler-drift](https://github.com/kessler-drift) on 2026-11-22T00:15:51Z. --- ### What `ures_getUTF8String`, `ures_getUTF8StringByIndex` and `ures_getUTF8StringByKey` read every type-0 resource at `pRoot + offset * 4`, including the resource word 0. ICU stores an empty string as that word, and `res_getString` maps it to a static empty string instead of reading the bundle ([ICU 68.2 `uresdata.cpp` l.335-338](https://github.com/unicode-org/icu/blob/84e1f26ea77152936e70d53178a816dbfbf69989/icu4c/source/common/uresdata.cpp#L335-L338): `res==0 ? &gEmptyString.length : pResData->pRoot+res`; ICU 78.1 has the same lines). `genrb` 68.2 writes `b { "" }` as resource 0 from format version 2 on. The accessors took `pRoot[0]`, the root resource of the bundle, as the string length: with a root table such as `0x20000040` they tried to copy 2^29 UTF-16 units past the end of the bundle and threw `NotImplemented`, and with a small `pRoot[0]` they returned bytes of the bundle. They now return the empty string for resource 0, as ICU does, through the existing empty-string path (`""` without `forceCopy`, a terminated `dest` with it). The debt entry of these accessors does not mention this case and is unchanged. ### 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. --> Ubuntu 26.04 x86-64 container under Rosetta on macOS 27.2, GCC 15.2, `main` `15826135019`. A harness compiles one resource file with `genrb` 68.2 in format versions 1 and 2, opens it with ICU 68.2 (`ures_openDirect`), copies the 200 bytes of each real `UResourceBundle` (the root, every item, nested items), and calls the three accessors on each object with ICU 68.2 and with this module (same bytes, `pRoot` pointed at the module's copy of the same `.res` file): every index or key, `pLength` null, capacities -1 to 4, 13, 37, 38, 109 and 150, `forceCopy` 0 and 1, incoming status 0, -124, -128 and 1, each call in a forked process. | Calls compared with ICU 68.2 | `main` | this PR | |---|---|---| | 18 900 calls, results that changed | | 106 | | the 106 calls that read resource 0 (format 2), equal to ICU 68.2 | 0 (throw) | 88 | The 18 remaining calls differ only by an incoming `U_STRING_NOT_TERMINATED_WARNING` that ICU clears when it terminates the output, which these accessors do not do for any string; that is a separate change. No other result changed. Two more resource files, generated at random from seeds 20261121 and 20261122 (36 288 and 18 900 calls), give the same picture: 583 and 318 results change, 484 and 264 of them now equal ICU 68.2, the others differ only by that incoming warning, and no other result changes. `guest_icu_resource_utf8` now puts a one-unit string at `pRoot[0]` and reads resource 0 through all three accessors (a bundle whose resource is 0, with and without `forceCopy`, and a table whose item is 0, by index and by key); it fails without the change. The 87 `libSceIcu` tests and the 91 `public_headers`, registration and progress tests pass, apart from the failures `main` already has under Rosetta. Not tested on a console, on Windows or on macOS. `python3 tools/check_conventions.py --base upstream/main` passes. `python3 tools/pr_overlap.py --names` for the three accessors lists no open pull request that changes `ResourceUtf8.cpp`; #24322, #24910 and #24917 split libraries, #14443, #19670 and #24288 change other resource files. ### 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
PrivacyOnion commented 2026-11-22 00:17:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-22T00:17:01Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 7fe662cb3d:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_icu_resource_utf8_tests built cleanly.
  • CTest (guest_icu_resource_utf8):
    1/1 Test #4141: guest_icu_resource_utf8 ..........   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libSceIcu/ResourceUtf8.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #4141: guest_icu_resource_utf8 ..........Subprocess aborted***Exception:   0.20 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified ReadString in core/libs/prx/libSceIcu/ResourceUtf8.cpp: resource == 0 check is performed after verifying StringType (since resource >> TypeShift == 0 for resource 0), clearing units and returning true.
    • Verified ToUtf8 empty units path: when units.empty(), copies out length 0 if length pointer is non-null, returns empty string literal when forceCopy == 0, and writes null terminator or StringNotTerminated when forceCopy != 0.
    • Verified ures_getUTF8String, ures_getUTF8StringByIndex, and ures_getUTF8StringByKey correctly handle resource 0 for root bundles, array elements, and table entries.
    • Verified tests in core/libs/prx/libSceIcu/tests/ResourceUtf8.cpp cover direct string bundle, forceCopy=0, forceCopy=1, ures_getUTF8StringByIndex, and ures_getUTF8StringByKey for resource 0.
    • Verified memory safety, guest memory copy safety via CopyIn/CopyOut, LP64/LLP64 portability, error code correctness, and strict AGENTS.md compliance (no comments, conventional commit PR title, clean checks).

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6009131619 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25371#pullrequestreview-6009131619). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-22T00:17:01Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `7fe662cb3d`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_icu_resource_utf8_tests` built cleanly. - **CTest** (`guest_icu_resource_utf8`): ``` 1/1 Test #4141: guest_icu_resource_utf8 .......... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libSceIcu/ResourceUtf8.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #4141: guest_icu_resource_utf8 ..........Subprocess aborted***Exception: 0.20 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified ReadString in core/libs/prx/libSceIcu/ResourceUtf8.cpp: resource == 0 check is performed after verifying StringType (since resource >> TypeShift == 0 for resource 0), clearing units and returning true. - Verified ToUtf8 empty units path: when units.empty(), copies out length 0 if length pointer is non-null, returns empty string literal when forceCopy == 0, and writes null terminator or StringNotTerminated when forceCopy != 0. - Verified ures_getUTF8String, ures_getUTF8StringByIndex, and ures_getUTF8StringByKey correctly handle resource 0 for root bundles, array elements, and table entries. - Verified tests in core/libs/prx/libSceIcu/tests/ResourceUtf8.cpp cover direct string bundle, forceCopy=0, forceCopy=1, ures_getUTF8StringByIndex, and ures_getUTF8StringByKey for resource 0. - Verified memory safety, guest memory copy safety via CopyIn/CopyOut, LP64/LLP64 portability, error code correctness, and strict AGENTS.md compliance (no comments, conventional commit PR title, clean checks). Looks Good To Me
throny commented 2026-11-22 02:12:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by throny on 2026-11-22T02:12:30Z.

Review: APPROVED


Review: ✅ correct

Basis: code read against the pinned ICU (3rdparty/icu)

Checked

  • ICU encodes the empty string as resource 0: res_getStringNoTrace uses res==0 ? &gEmptyString.length : pResData->pRoot+res (uresdata.cpp:336), and ures_getUTF8String reaches it through ures_getString. RES_BOGUS is 0xffffffff, never 0.
  • On main, ReadString read a length from pRoot[0], the root resource word, so an empty-string resource came back as garbage or a failed copy. All three exports (ures_getUTF8String, ByIndex, ByKey) go through ReadString, so the one guard covers them.
  • The new fixtures don't overlap existing ones (earlier stores end at byte 111), and the bait pool[0]=1, 'z' makes the test fail on main.

Findings: none.

Suggested action: merge.

AI-assisted review (Claude Code) · head 7fe662cb3d · base main 851212fb98

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6009683457 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25371#pullrequestreview-6009683457). Originally posted by [throny](https://github.com/throny) on 2026-11-22T02:12:30Z. **Review:** APPROVED --- ### Review: ✅ correct **Basis:** code read against the pinned ICU (`3rdparty/icu`) **Checked** - ICU encodes the empty string as resource 0: `res_getStringNoTrace` uses `res==0 ? &gEmptyString.length : pResData->pRoot+res` (`uresdata.cpp:336`), and `ures_getUTF8String` reaches it through `ures_getString`. `RES_BOGUS` is `0xffffffff`, never 0. - On `main`, `ReadString` read a length from `pRoot[0]`, the root resource word, so an empty-string resource came back as garbage or a failed copy. All three exports (`ures_getUTF8String`, `ByIndex`, `ByKey`) go through `ReadString`, so the one guard covers them. - The new fixtures don't overlap existing ones (earlier stores end at byte 111), and the bait `pool[0]=1`, `'z'` makes the test fail on `main`. **Findings:** none. **Suggested action:** merge. <sub>AI-assisted review (Claude Code) · head `7fe662cb3d` · base `main` `851212fb98`</sub>
MarcZX10 commented 2026-11-22 05:37:15 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the reviewed code and reported checks support merging.

What I ran

Reviewed head 7fe662cb3d4b7c8b7cbc58a8020c7288db93a5ca. No commands or tests were run in this review. Read description.md, commits.md, manifest.json, diff.patch, and both changed files at the pinned head: core/libs/prx/libSceIcu/ResourceUtf8.cpp and core/libs/prx/libSceIcu/tests/ResourceUtf8.cpp.

Results

Check Result Note
Code review passed Resource-zero handling matches the described case
Tests in this review not run Author reports tests and harness results

Findings

Blocking

None.

Non-blocking

None.

Not verified

Did not run the reported tests, harness, or convention check; their results are author-reported. Console, Windows, and macOS behavior were not tested. No files were excluded from review; files.json was unavailable, but manifest.json lists both changed files as included and both were read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6010569608 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25371#pullrequestreview-6010569608). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T05:37:15Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the reviewed code and reported checks support merging. ## What I ran Reviewed head `7fe662cb3d4b7c8b7cbc58a8020c7288db93a5ca`. No commands or tests were run in this review. Read `description.md`, `commits.md`, `manifest.json`, `diff.patch`, and both changed files at the pinned head: `core/libs/prx/libSceIcu/ResourceUtf8.cpp` and `core/libs/prx/libSceIcu/tests/ResourceUtf8.cpp`. ## Results | Check | Result | Note | |---|---|---| | Code review | passed | Resource-zero handling matches the described case | | Tests in this review | not run | Author reports tests and harness results | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Did not run the reported tests, harness, or convention check; their results are author-reported. Console, Windows, and macOS behavior were not tested. No files were excluded from review; `files.json` was unavailable, but `manifest.json` lists both changed files as included and both were read. ## To get this to a good state Nothing is required.
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!25371
No description provided.