fix(libSceNpCommon): copy at most the length limit in sceNpStrncpyToBuf #14318

Merged
MarcZX10 merged 2 commits from upstream-pr/14318 into main 2026-11-02 19:48:11 +00:00
MarcZX10 commented 2026-11-01 09:59:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-01T09:59:13Z.


What

sceNpStrncpyToBuf can copy more than maxLength characters and read the source past maxLength. CopyToBuffer checks the destination size against the string length, then calls strncpy(dst, src, size - 1). When the source has no terminator within maxLength, strncpy keeps reading the source until it finds a NUL or fills the destination, so the copy can exceed the length the caller passed.

The fix copies exactly length bytes and zero-fills the rest of the destination up to size - 1, then writes the terminator through the same zero-fill. For every input whose source ends within maxLength, the output bytes are identical to the previous code. That covers sceNpStrcpyToBuf too, which calls the same helper.

Commits:

  • 565e2dc60b: the copy helper in core/libs/prx/libSceNpCommon/Strings.cpp.
  • 6739179eed: TestHexAndCopies in core/libs/tests/GuestNpCommonRuntime.cpp copies a source whose first terminator comes after the limit and expects the first four characters.

Tested

Linux, archive build of upstream/main 5c9837bfe7 plus these commits, built with the validation helper in /marc/AnyPS5/.git/info/tools/run_tests.sh. That helper patches the local CPU startup gate in the throwaway copy only.

  • ctest -R '^guest_np_common_runtime$' --output-on-failure: 1 of 1 passed, on the fixed tree (BUILD-EXIT 0).
  • Negative control: the same test source built against the previous copy helper (strncpy(dst, src, size - 1)). The new check aborts with a source without a terminator within the limit is cut at the limit, and ctest reports 0 of 1 passed.
  • python3 tools/check_conventions.py --base upstream/main and git diff --check upstream/main: clean.
  • Overlap: tools/pr_overlap.py --branch fails closed on five open PRs that touch GuestNpCommonRuntime.cpp or Strings.cpp: #12818, #12852, #13130, #13270 and #13277. Compared by hand: each PR's test and strings hunks were split per file and checked with patch --dry-run -F0 against main and against this branch's files. They apply to this branch wherever they apply to main. #12852 fails on main too, so that is base drift. #12818 changes the XML code in Strings.cpp, not CopyToBuffer. No patch names CopyToBuffer; the only matches for strncpy, TestHexAndCopies or unterminated are in #12852's own test in TestObjects. That is evidence, not proof.
  • Precheck: its ABI audit row fails only because the precheck's temporary head worktree has no tools/pr_abi_audit.py. Run by hand from the main checkout on this head, tools/pr_abi_audit.py --base upstream/main --head 6739179eed --json reports "status": "passed". The test-quality helper, also run by hand, reports one possible-unregistered-cpp-test finding for GuestNpCommonRuntime.cpp. That is a false positive: the test is registered as add_test_executable(guest_np_common_runtime_tests tests/GuestNpCommonRuntime.cpp) in core/libs/CMakeLists.txt, relative to core/libs, and it ran in the archive build.

Not run: Windows and macOS builds, a title or console run, and the null-argument paths, which this change does not touch.

Checklist

  • Based on current main
  • 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:5105916585 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14318). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-01T09:59:13Z. --- ### What `sceNpStrncpyToBuf` can copy more than `maxLength` characters and read the source past `maxLength`. `CopyToBuffer` checks the destination size against the string length, then calls `strncpy(dst, src, size - 1)`. When the source has no terminator within `maxLength`, `strncpy` keeps reading the source until it finds a NUL or fills the destination, so the copy can exceed the length the caller passed. The fix copies exactly `length` bytes and zero-fills the rest of the destination up to `size - 1`, then writes the terminator through the same zero-fill. For every input whose source ends within `maxLength`, the output bytes are identical to the previous code. That covers `sceNpStrcpyToBuf` too, which calls the same helper. Commits: - `565e2dc60b`: the copy helper in `core/libs/prx/libSceNpCommon/Strings.cpp`. - `6739179eed`: `TestHexAndCopies` in `core/libs/tests/GuestNpCommonRuntime.cpp` copies a source whose first terminator comes after the limit and expects the first four characters. ### Tested Linux, archive build of `upstream/main` `5c9837bfe7` plus these commits, built with the validation helper in `/marc/AnyPS5/.git/info/tools/run_tests.sh`. That helper patches the local CPU startup gate in the throwaway copy only. - `ctest -R '^guest_np_common_runtime$' --output-on-failure`: 1 of 1 passed, on the fixed tree (`BUILD-EXIT 0`). - Negative control: the same test source built against the previous copy helper (`strncpy(dst, src, size - 1)`). The new check aborts with `a source without a terminator within the limit is cut at the limit`, and `ctest` reports 0 of 1 passed. - `python3 tools/check_conventions.py --base upstream/main` and `git diff --check upstream/main`: clean. - Overlap: `tools/pr_overlap.py --branch` fails closed on five open PRs that touch `GuestNpCommonRuntime.cpp` or `Strings.cpp`: #12818, #12852, #13130, #13270 and #13277. Compared by hand: each PR's test and strings hunks were split per file and checked with `patch --dry-run -F0` against `main` and against this branch's files. They apply to this branch wherever they apply to `main`. #12852 fails on `main` too, so that is base drift. #12818 changes the XML code in `Strings.cpp`, not `CopyToBuffer`. No patch names `CopyToBuffer`; the only matches for `strncpy`, `TestHexAndCopies` or `unterminated` are in #12852's own test in `TestObjects`. That is evidence, not proof. - Precheck: its ABI audit row fails only because the precheck's temporary head worktree has no `tools/pr_abi_audit.py`. Run by hand from the main checkout on this head, `tools/pr_abi_audit.py --base upstream/main --head 6739179eed --json` reports `"status": "passed"`. The test-quality helper, also run by hand, reports one `possible-unregistered-cpp-test` finding for `GuestNpCommonRuntime.cpp`. That is a false positive: the test is registered as `add_test_executable(guest_np_common_runtime_tests tests/GuestNpCommonRuntime.cpp)` in `core/libs/CMakeLists.txt`, relative to `core/libs`, and it ran in the archive build. Not run: Windows and macOS builds, a title or console run, and the null-argument paths, which this change does not touch. ### Checklist - [x] Based on current `main` - [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
BunnyFl commented 2026-11-02 01:08:31 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Read against main: sceNpStrncpyToBuf passes length = strnlen(src, maxLength) (Strings.cpp:478), but strncpy(dst, src, size - 1) copied until the source's terminator, past maxLength. With the new test's input ("ABCDEF", limit 4, size 8), main writes "ABCDEF" and the PR writes "ABCD". memcpy(length) + memset(dst + length, 0, size - length) stays within size, keeps the old zero padding, and leaves sceNpStrcpyToBuf (length = strlen) byte-identical. Looks correct. I haven't built or run it yet.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5731157285 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14318#pullrequestreview-5731157285). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T01:08:31Z. **Review:** COMMENTED --- Read against main: `sceNpStrncpyToBuf` passes `length = strnlen(src, maxLength)` (`Strings.cpp:478`), but `strncpy(dst, src, size - 1)` copied until the source's terminator, past `maxLength`. With the new test's input (`"ABCDEF"`, limit 4, size 8), main writes `"ABCDEF"` and the PR writes `"ABCD"`. `memcpy(length)` + `memset(dst + length, 0, size - length)` stays within `size`, keeps the old zero padding, and leaves `sceNpStrcpyToBuf` (`length = strlen`) byte-identical. Looks correct. I haven't built or run it yet.
BunnyFl commented 2026-11-02 01:16:02 +00:00 (Migrated from github.com)

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

Review: APPROVED


Follow-up to my earlier comment: I've now run it.

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

  • guest_np_common_runtime passes.
  • Negative control: the same test built against main's Strings.cpp aborts with a source without a terminator within the limit is cut at the limit (0xc0000409), so the new check covers the change.

With the read-through above (length = strnlen(src, maxLength), the copy and zero fill stay within size, sceNpStrcpyToBuf output unchanged), this looks good to merge.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5731187688 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14318#pullrequestreview-5731187688). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T01:16:02Z. **Review:** APPROVED --- Follow-up to my earlier comment: I've now run it. Windows 11, WinLibs GCC 15.2.0 r7, Release. Head 6739179eed merged into upstream/main 93dbd0f82a: - `guest_np_common_runtime` passes. - Negative control: the same test built against main's `Strings.cpp` aborts with `a source without a terminator within the limit is cut at the limit` (0xc0000409), so the new check covers the change. With the read-through above (`length = strnlen(src, maxLength)`, the copy and zero fill stay within `size`, `sceNpStrcpyToBuf` output unchanged), this looks good to merge.
MarcZX10 commented 2026-11-02 06:21:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T06:21:12Z.

Review: COMMENTED


Reviewed — no blocking findings

CopyToBuffer now copies exactly length bytes and zero-fills the rest of the destination. Before, strncpy copied up to size - 1 bytes, which could go past the caller's length limit when the source was longer than the limit. Both callers pass a length that stops at the first NUL, so the change affects only that case. The new test covers it. No hosted job has run this head: it has no Windows check-run at all.

What I ran

  • Head 6739179eedd24b1a8bb5f1f8262281ac485574c3; base 26fa1212b82f76dd49cf12d65e445d6ff3e0844c. 2 files: libSceNpCommon/Strings.cpp (the function body) and tests/GuestNpCommonRuntime.cpp (one new check).
  • Read the function diff in full. Checked the two callers in Strings.cpp: CopyToBuffer(dst, size, src, std::strlen(src)) and CopyToBuffer(dst, size, src, strnlen(src, maxLength)). So length never includes a NUL. Checked the bounds: the size check keeps length + 1 <= size, and dst + length + (size - length) is dst + size.
  • Read the new test check: sceNpStrncpyToBuf(copy, 8, "ABCDEF\0", 4) must give "ABCD".
  • Hosted: the hosted summary reports no Windows check-run for this head, and no run of GuestNpCommonRuntime exists anywhere for it.
  • Read-only checks from the trusted main checkout: diff check, conventions and PR template, and the stack check all passed. The ABI audit passed: 26 symbols, no findings.
  • No MarcZX10 review at this head.

Results

Check Result Note
Diff check passed
Conventions and PR template passed conventions check from the main checkout
Mergeability and dependencies passed stack check passed; no declared dependencies
Callers pass a length that excludes any NUL passed by reading strlen and strnlen
Bounds of the copy and the zero fill passed by reading length + 1 <= size; the fill ends at size
Behaviour for a source longer than the limit passed by reading copies exactly length, as the title says
New test check (GuestNpCommonRuntime) not run no Windows check-run and no run of this test on this head
ABI audit (main-checkout tool) passed 26 symbols, no findings
Linux build and runtime tests not run Linux is skipped on pull requests
Negative controls not run no local build
Hosted CI not available conventions, eligibility, intake, labels, library-portability and report succeeded; no Windows, Linux or relinker run on this head
Read-only check output

Precheck was not run: no PR-head code is run here. Diff check: passed. Conventions: passed. ABI audit: passed (26 symbols). Stack check: passed. Hosted: gh api repos/boykopovar/AnyPS5/commits/6739179eedd24b1a8bb5f1f8262281ac485574c3/check-runs.

Negative control

Not run here. A negative control needs a local build, and code from this head is not run here.

Findings

Blocking

None.

Non-blocking

  • No hosted job has run this head's changes. The Windows check-run is absent, so the new check in GuestNpCommonRuntime.cpp has no result anywhere. A run at this head would settle it.

Sources

Not verified

  • Whether the new check passes.
  • The console behaviour of sceNpStrncpyToBuf for a source longer than the limit.

To get this to a good state

  • Nothing blocking. A run of the guest test at this head would confirm the new check.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5732763105 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14318#pullrequestreview-5732763105). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T06:21:12Z. **Review:** COMMENTED --- # Reviewed — no blocking findings `CopyToBuffer` now copies exactly `length` bytes and zero-fills the rest of the destination. Before, `strncpy` copied up to `size - 1` bytes, which could go past the caller's length limit when the source was longer than the limit. Both callers pass a length that stops at the first NUL, so the change affects only that case. The new test covers it. No hosted job has run this head: it has no Windows check-run at all. ## What I ran - Head `6739179eedd24b1a8bb5f1f8262281ac485574c3`; base `26fa1212b82f76dd49cf12d65e445d6ff3e0844c`. 2 files: `libSceNpCommon/Strings.cpp` (the function body) and `tests/GuestNpCommonRuntime.cpp` (one new check). - Read the function diff in full. Checked the two callers in `Strings.cpp`: `CopyToBuffer(dst, size, src, std::strlen(src))` and `CopyToBuffer(dst, size, src, strnlen(src, maxLength))`. So `length` never includes a NUL. Checked the bounds: the size check keeps `length + 1 <= size`, and `dst + length + (size - length)` is `dst + size`. - Read the new test check: `sceNpStrncpyToBuf(copy, 8, "ABCDEF\0", 4)` must give `"ABCD"`. - Hosted: the hosted summary reports no Windows check-run for this head, and no run of `GuestNpCommonRuntime` exists anywhere for it. - Read-only checks from the trusted main checkout: diff check, conventions and PR template, and the stack check all passed. The ABI audit passed: 26 symbols, no findings. - No MarcZX10 review at this head. ## Results | Check | Result | Note | | --- | --- | --- | | Diff check | passed | | | Conventions and PR template | passed | conventions check from the main checkout | | Mergeability and dependencies | passed | stack check passed; no declared dependencies | | Callers pass a length that excludes any NUL | passed by reading | `strlen` and `strnlen` | | Bounds of the copy and the zero fill | passed by reading | `length + 1 <= size`; the fill ends at `size` | | Behaviour for a source longer than the limit | passed by reading | copies exactly `length`, as the title says | | New test check (`GuestNpCommonRuntime`) | not run | no Windows check-run and no run of this test on this head | | ABI audit (main-checkout tool) | passed | 26 symbols, no findings | | Linux build and runtime tests | not run | Linux is skipped on pull requests | | Negative controls | not run | no local build | | Hosted CI | not available | conventions, eligibility, intake, labels, library-portability and report succeeded; no Windows, Linux or relinker run on this head | <details><summary>Read-only check output</summary> Precheck was not run: no PR-head code is run here. Diff check: passed. Conventions: passed. ABI audit: passed (26 symbols). Stack check: passed. Hosted: `gh api repos/boykopovar/AnyPS5/commits/6739179eedd24b1a8bb5f1f8262281ac485574c3/check-runs`. </details> ## Negative control Not run here. A negative control needs a local build, and code from this head is not run here. ## Findings **Blocking** None. **Non-blocking** - No hosted job has run this head's changes. The Windows check-run is absent, so the new check in `GuestNpCommonRuntime.cpp` has no result anywhere. A run at this head would settle it. ## Sources - Function: https://github.com/boykopovar/AnyPS5/blob/6739179eedd24b1a8bb5f1f8262281ac485574c3/core/libs/prx/libSceNpCommon/Strings.cpp (lines 191-196; callers at 432 and 436) - Test: https://github.com/boykopovar/AnyPS5/blob/6739179eedd24b1a8bb5f1f8262281ac485574c3/core/libs/tests/GuestNpCommonRuntime.cpp (`TestHexAndCopies`) ## Not verified - Whether the new check passes. - The console behaviour of `sceNpStrncpyToBuf` for a source longer than the limit. ## To get this to a good state - Nothing blocking. A run of the guest test at this head would confirm the new check.
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!14318
No description provided.