fix(libc): reject a wcsncat_s copy only when its written and read ranges overlap #9290

Merged
thuanquang merged 2 commits from upstream-pr/9290 into main 2026-10-23 18:29:30 +00:00
thuanquang commented 2026-10-21 17:29:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-21T17:29:18Z.


What

wcsncat_s refuses a copy as overlapping when the source lies within length + 1 units of the write position (Strings.cpp:852-858 on main), so it also refuses copies whose ranges don't meet. @oneandonlydean read the console's own wcsncat_s in libc.prx (the module a retail title ships; read, not run on a console): it rejects a copy only when the bytes it writes, [dest + len1, dest + len1 + len2), intersect the bytes it reads, [src, src + len2). For "abc" with src = dest + 1 and count = 2 it writes indices 3-4 and reads 1-2, so the console copies, returns 0 and leaves "abcbc", while main returns EINVAL (22) and clears dest[0]. This takes the console's rule. He asked for it as a separate PR on #8269 (comment).

  • The length check and its ERANGE (34) are unchanged; the same reading found they match.
  • wcscat_s calls wcsncat_s with an unbounded count, so it gets the same rule; its own console code was not read.
  • wmemcpy_s (Strings.cpp:776) and wcsncpy_s (:817) have similar distance checks; they are left as they are, since nobody has read them on the console.
  • #8965 (open) changes this function's size-limit check, not the overlap check; the two merge cleanly.
  • docs/dev/debt/libc.md gets an entry: the rule follows a static reading of the console's wcsncat_s (not run on a console), wcscat_s inherits it unread, and main's narrow strncat_s keeps a wider rule ([dest, dest + used + length + 1) against [src, src + length + 1), from a998987278), so it refuses some copies whose wide equivalent now proceeds.

Tested

Windows 11 26200, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. Rebased onto main 30ecc1b7b3 to add the debt entry (the only conflict was that entry's neighbour in docs/dev/debt/libc.md; the code and test are unchanged): with this change's Strings.cpp and test applied to main 9d6adeb550, guest_wide_strings passes 3 of 3. The runs below are from main 4ec064d29.

  • guest_wide_strings gains @oneandonlydean's two cases: the non-overlapping copy above (returns 0, leaves "abcbc"), and u"abc\0xyz" with src = dest + 4 and count = 3, where the read range starts inside the write range (returns 22 with dest[0] cleared).
  • Green: passes 3 of 3 with this change.
  • Red: with main's Strings.cpp and the new test, the test aborts (exit 0xc0000409). With the first new row removed it passes, so the non-overlapping copy is the row main fails.
  • Control: with the overlap check removed from this change, the test fails, so the second row pins the rejection.
  • python tools/check_conventions.py --base origin/main with this title and body passes.
  • @xXPBSXx on Linux (WSL2 Ubuntu 24.04, GCC 13.3, Release), this head merged onto main df2569d2c5: guest_wide_strings exits 0 with the change; with main's Strings.cpp and the PR's tests it aborts (SIGABRT, exit 134).
  • Not tested: on a console; the other _s functions' overlap rules.

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:4955677050 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9290). Originally posted by [thuanquang](https://github.com/thuanquang) on 2026-10-21T17:29:18Z. --- ### What `wcsncat_s` refuses a copy as overlapping when the source lies within `length + 1` units of the write position (`Strings.cpp:852-858` on `main`), so it also refuses copies whose ranges don't meet. @oneandonlydean [read the console's own `wcsncat_s`](https://github.com/boykopovar/AnyPS5/pull/8269#issuecomment-6297933352) in `libc.prx` (the module a retail title ships; read, not run on a console): it rejects a copy only when the bytes it writes, `[dest + len1, dest + len1 + len2)`, intersect the bytes it reads, `[src, src + len2)`. For `"abc"` with `src = dest + 1` and `count = 2` it writes indices 3-4 and reads 1-2, so the console copies, returns 0 and leaves `"abcbc"`, while `main` returns `EINVAL` (22) and clears `dest[0]`. This takes the console's rule. He asked for it as a separate PR on #8269 ([comment](https://github.com/boykopovar/AnyPS5/pull/8269#issuecomment-6315846529)). - The length check and its `ERANGE` (34) are unchanged; the same reading found they match. - `wcscat_s` calls `wcsncat_s` with an unbounded count, so it gets the same rule; its own console code was not read. - `wmemcpy_s` (`Strings.cpp:776`) and `wcsncpy_s` (`:817`) have similar distance checks; they are left as they are, since nobody has read them on the console. - #8965 (open) changes this function's size-limit check, not the overlap check; the two merge cleanly. - `docs/dev/debt/libc.md` gets an entry: the rule follows a static reading of the console's `wcsncat_s` (not run on a console), `wcscat_s` inherits it unread, and `main`'s narrow `strncat_s` keeps a wider rule (`[dest, dest + used + length + 1)` against `[src, src + length + 1)`, from a998987278), so it refuses some copies whose wide equivalent now proceeds. ### Tested Windows 11 26200, WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`. Rebased onto `main` `30ecc1b7b3` to add the debt entry (the only conflict was that entry's neighbour in `docs/dev/debt/libc.md`; the code and test are unchanged): with this change's `Strings.cpp` and test applied to `main` `9d6adeb550`, `guest_wide_strings` passes 3 of 3. The runs below are from `main` `4ec064d29`. - `guest_wide_strings` gains @oneandonlydean's two cases: the non-overlapping copy above (returns 0, leaves `"abcbc"`), and `u"abc\0xyz"` with `src = dest + 4` and `count = 3`, where the read range starts inside the write range (returns 22 with `dest[0]` cleared). - Green: passes 3 of 3 with this change. - Red: with `main`'s `Strings.cpp` and the new test, the test aborts (exit `0xc0000409`). With the first new row removed it passes, so the non-overlapping copy is the row `main` fails. - Control: with the overlap check removed from this change, the test fails, so the second row pins the rejection. - `python tools/check_conventions.py --base origin/main` with this title and body passes. - @xXPBSXx [on Linux](https://github.com/boykopovar/AnyPS5/pull/9290#issuecomment-6332491115) (WSL2 Ubuntu 24.04, GCC 13.3, Release), this head merged onto `main` `df2569d2c5`: `guest_wide_strings` exits 0 with the change; with `main`'s `Strings.cpp` and the PR's tests it aborts (SIGABRT, exit 134). - Not tested: on a console; the other `_s` functions' overlap rules. ### 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)
xXPBSXx commented 2026-10-22 04:11:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-22T04:11:28Z.


Linux data point for edd4ae2936 (WSL2, Ubuntu 24.04, kernel 6.18 - a virtualised kernel, not bare-metal Linux; GCC 13.3, Release, BUILD_TESTING=ON), this head merged onto main df2569d2c5 (clean merge). I built guest_wide_string_tests and ran the executable directly (exit code, not through ctest):

  • With the PR: exit code 0.
  • Without it (the PR's test sources on top of main, with core/libs/prx/libc/src/Strings.cpp reset to main): it aborts (SIGABRT, exit code 134).
    Not tested: macOS, Windows, the rest of the test suite.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6332491115 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9290#issuecomment-6332491115). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-22T04:11:28Z. --- Linux data point for `edd4ae2936` (WSL2, Ubuntu 24.04, kernel 6.18 - a virtualised kernel, not bare-metal Linux; GCC 13.3, Release, `BUILD_TESTING=ON`), this head merged onto `main` `df2569d2c5` (clean merge). I built `guest_wide_string_tests` and ran the executable directly (exit code, not through `ctest`): - With the PR: exit code 0. - Without it (the PR's test sources on top of `main`, with `core/libs/prx/libc/src/Strings.cpp` reset to `main`): it aborts (SIGABRT, exit code 134). Not tested: macOS, Windows, the rest of the test suite. AI-assisted (Claude Code).
harisibrar1 commented 2026-10-22 18:15:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T18:15:11Z.

Review: COMMENTED


Checked against main a6646f8464 and the console-module reading of wcsncat_s on #8269:

  • The new condition is exactly the write range [dest+used, dest+used+length) against the read range [src, src+length). I worked both new rows through it by hand, and a zero-length copy still never rejects, as on main. The head merges cleanly with main.
  • Could you add a debt line in docs/dev/debt/libc.md? It would say that the wcsncat_s overlap rule follows a static reading of libc.prx (not run on a console) and that wcscat_s inherits it without its own code being read. The PR doesn't touch the debt file, and the existing _s entry only covers the size limits.
  • Main's narrow strncat_s uses a wider rule ([dest, dest+used+length+1) against [src, src+length+1), from a998987278), and GuestStrings.cpp:137 pins strncat_s(catOverlap + 3, 10, catOverlap, 3) == 22, where the written bytes [6,9) and the read bytes [0,3) don't meet. With this PR, the wide equivalent of that call copies. That's fine if the console really differs, but it may be worth noting in the debt file, or opening an issue so someone reads the narrow function's console code too.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5618820757 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9290#pullrequestreview-5618820757). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T18:15:11Z. **Review:** COMMENTED --- Checked against main a6646f8464 and the console-module reading of `wcsncat_s` on #8269: - The new condition is exactly the write range `[dest+used, dest+used+length)` against the read range `[src, src+length)`. I worked both new rows through it by hand, and a zero-length copy still never rejects, as on main. The head merges cleanly with main. - Could you add a debt line in `docs/dev/debt/libc.md`? It would say that the `wcsncat_s` overlap rule follows a static reading of `libc.prx` (not run on a console) and that `wcscat_s` inherits it without its own code being read. The PR doesn't touch the debt file, and the existing `_s` entry only covers the size limits. - Main's narrow `strncat_s` uses a wider rule (`[dest, dest+used+length+1)` against `[src, src+length+1)`, from a998987278), and `GuestStrings.cpp:137` pins `strncat_s(catOverlap + 3, 10, catOverlap, 3) == 22`, where the written bytes [6,9) and the read bytes [0,3) don't meet. With this PR, the wide equivalent of that call copies. That's fine if the console really differs, but it may be worth noting in the debt file, or opening an issue so someone reads the narrow function's console code too. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9290
No description provided.