test(libc): cover wcsncat_s exact fit, one too many and unterminated destination #8269

Merged
thuanquang merged 2 commits from upstream-pr/8269 into main 2026-10-21 14:09:32 +00:00
thuanquang commented 2026-10-20 07:55:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-20T07:55:40Z.


What

A follow-up @oneandonlydean listed on #6144 (comment): the guest_wide_strings test does not cover these wcsncat_s cases. This adds them; no code changes.

  • Exact fit: appending to abc in an 8-unit buffer succeeds with 1234 (7 units and the terminator), and 12345 gives ERANGE (34) with dest[0] cleared.
  • Unterminated destination: four non-null units in a 4-unit buffer give ERANGE with dest[0] cleared.

@oneandonlydean compared the cases with the console's own wcsncat_s in libc.prx (read from the module a retail title ships, not run on a console): these three match it. The first head also had an overlapping-source row (dest + 1 appended to abc with count 2, expecting EINVAL). The console copies that call and returns 0, because it rejects only when the written range [dest + len1, dest + len1 + len2) meets the read range [src, src + len2), so the row would have pinned a false positive in main's check. It is dropped; aligning main's check with the console is a separate change.

The rows sit after the existing wcsncat_s rows, away from #7952's wcscpy_s rows.

Tested

Windows 11 26200, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON.

  • guest_wide_strings passes 3 of 3 on main 47fa6eecf with this head (a936eadd6); the first head passed 3 of 3 on main 1edbe4b5e.
  • Mutation control on the first head, rebuilt and run, then reverted: with wcsncat_s's length check off by one (length > destsz - used), the test aborts on the exact-fit require. None of the existing rows appends exactly up to destsz - 1.
  • python tools/check_conventions.py --base origin/main with this title and body passes.

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 (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4932750620 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8269). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-20T07:55:40Z. --- ### What A follow-up @oneandonlydean listed on #6144 ([comment](https://github.com/boykopovar/AnyPS5/pull/6144#issuecomment-6244123487)): the `guest_wide_strings` test does not cover these `wcsncat_s` cases. This adds them; no code changes. - Exact fit: appending to `abc` in an 8-unit buffer succeeds with `1234` (7 units and the terminator), and `12345` gives `ERANGE` (34) with `dest[0]` cleared. - Unterminated destination: four non-null units in a 4-unit buffer give `ERANGE` with `dest[0]` cleared. @oneandonlydean [compared the cases](https://github.com/boykopovar/AnyPS5/pull/8269#issuecomment-6297933352) with the console's own `wcsncat_s` in `libc.prx` (read from the module a retail title ships, not run on a console): these three match it. The first head also had an overlapping-source row (`dest + 1` appended to `abc` with `count` 2, expecting `EINVAL`). The console copies that call and returns 0, because it rejects only when the written range `[dest + len1, dest + len1 + len2)` meets the read range `[src, src + len2)`, so the row would have pinned a false positive in `main`'s check. It is dropped; aligning `main`'s check with the console is a separate change. The rows sit after the existing `wcsncat_s` rows, away from #7952's `wcscpy_s` rows. ### Tested Windows 11 26200, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`. - `guest_wide_strings` passes 3 of 3 on `main` `47fa6eecf` with this head (`a936eadd6`); the first head passed 3 of 3 on `main` `1edbe4b5e`. - Mutation control on the first head, rebuilt and run, then reverted: with `wcsncat_s`'s length check off by one (`length > destsz - used`), the test aborts on the exact-fit `require`. None of the existing rows appends exactly up to `destsz - 1`. - `python tools/check_conventions.py --base origin/main` with this title and body passes. ### 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 (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-20 08:08:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T08:08:18Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6044 core/libs/tests/GuestWideStrings.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6286458084 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8269#issuecomment-6286458084). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T08:08:18Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestWideStrings.cpp` |
oneandonlydean commented 2026-10-20 18:16:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T18:16:49Z.


Thanks. I compared these cases with the console's own wcsncat_s in libc.prx, read from the module that a retail title (PPSA21564) ships. Three of the four new lines match it, but the overlap case doesn't:

  • Exact fit ("abc" + "1234" in 8): the console returns 0. Matches.
  • One too many ("abc" + "12345" in 8) and unterminated destination (4 × 'x' in 4): the console computes wcsnlen(dest, destsz) + wcsnlen(src, count), and when that is >= destsz it sets dest[0] = 0 and returns 34 (ERANGE). Both match.
  • Overlap ("abc", src = strBuf + 1, count = 2): the console rejects a copy only when the bytes it writes, [dest + len1, dest + len1 + len2), intersect the bytes it reads, [src, src + len2). Here it writes indices 3-4 and reads 1-2, so they don't intersect. The console copies, returns 0 and leaves "abcbc". Main's check compares the raw distance with (length + 1) * 2, so it reports 22 for this call, and the new line would pin that false positive.

Suggested shape: drop the overlap line, or (better) align main's check with the console and test both sides of it, e.g. the call above returns 0 with "abcbc", while u"abc\0xyz" with src = strBuf + 4, count = 3 (the read range starts inside the write range) returns 22 with dest[0] == 0.

This comes from reading the module, not from running on a console.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6297933352 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8269#issuecomment-6297933352). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T18:16:49Z. --- Thanks. I compared these cases with the console's own `wcsncat_s` in `libc.prx`, read from the module that a retail title (PPSA21564) ships. Three of the four new lines match it, but the overlap case doesn't: - **Exact fit** (`"abc"` + `"1234"` in 8): the console returns 0. Matches. - **One too many** (`"abc"` + `"12345"` in 8) and **unterminated destination** (4 × `'x'` in 4): the console computes `wcsnlen(dest, destsz) + wcsnlen(src, count)`, and when that is `>= destsz` it sets `dest[0] = 0` and returns 34 (`ERANGE`). Both match. - **Overlap** (`"abc"`, `src = strBuf + 1`, `count = 2`): the console rejects a copy only when the bytes it writes, `[dest + len1, dest + len1 + len2)`, intersect the bytes it reads, `[src, src + len2)`. Here it writes indices 3-4 and reads 1-2, so they don't intersect. The console copies, returns 0 and leaves `"abcbc"`. Main's check compares the raw distance with `(length + 1) * 2`, so it reports 22 for this call, and the new line would pin that false positive. Suggested shape: drop the overlap line, or (better) align main's check with the console and test both sides of it, e.g. the call above returns 0 with `"abcbc"`, while `u"abc\0xyz"` with `src = strBuf + 4`, `count = 3` (the read range starts inside the write range) returns 22 with `dest[0] == 0`. This comes from reading the module, not from running on a console.
thuanquang commented 2026-10-21 12:15:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-21T12:15:15Z.


@oneandonlydean thanks for checking these against the console's libc.prx, and sorry this went unanswered. I took the first option here: a936eadd6 drops the overlap row, so the test no longer pins main's false positive, and the three rows you confirmed stay. guest_wide_strings passes 3 of 3 on main 47fa6eecf with this head. Aligning main's check with the console (reject only when [dest + len1, dest + len1 + len2) intersects [src, src + len2), with both of your cases as tests) is a separate fix, and I'll open it as its own PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6315654350 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8269#issuecomment-6315654350). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-21T12:15:15Z. --- @oneandonlydean thanks for checking these against the console's `libc.prx`, and sorry this went unanswered. I took the first option here: `a936eadd6` drops the overlap row, so the test no longer pins `main`'s false positive, and the three rows you confirmed stay. `guest_wide_strings` passes 3 of 3 on `main` `47fa6eecf` with this head. Aligning `main`'s check with the console (reject only when `[dest + len1, dest + len1 + len2)` intersects `[src, src + len2)`, with both of your cases as tests) is a separate fix, and I'll open it as its own PR.
oneandonlydean commented 2026-10-21 12:25:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-21T12:25:34Z.


@thuanquang thanks, no problem. Checked on a936eadd6: it removes only the overlap row (and the reset after it), and the three console-confirmed rows stay. With that, the test pins only what the console does. A separate PR for main's overlap check sounds right. This one is ready for CI on our side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6315846529 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8269#issuecomment-6315846529). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-21T12:25:34Z. --- @thuanquang thanks, no problem. Checked on a936eadd6: it removes only the overlap row (and the reset after it), and the three console-confirmed rows stay. With that, the test pins only what the console does. A separate PR for `main`'s overlap check sounds right. This one is ready for CI on our side.
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!8269
No description provided.