fix(libc): reject a wcsncat_s copy only when its written and read ranges overlap #9290
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9290"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by thuanquang on 2026-10-21T17:29:18Z.
What
wcsncat_srefuses a copy as overlapping when the source lies withinlength + 1units of the write position (Strings.cpp:852-858onmain), so it also refuses copies whose ranges don't meet. @oneandonlydean read the console's ownwcsncat_sinlibc.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"withsrc = dest + 1andcount = 2it writes indices 3-4 and reads 1-2, so the console copies, returns 0 and leaves"abcbc", whilemainreturnsEINVAL(22) and clearsdest[0]. This takes the console's rule. He asked for it as a separate PR on #8269 (comment).ERANGE(34) are unchanged; the same reading found they match.wcscat_scallswcsncat_swith an unbounded count, so it gets the same rule; its own console code was not read.wmemcpy_s(Strings.cpp:776) andwcsncpy_s(:817) have similar distance checks; they are left as they are, since nobody has read them on the console.docs/dev/debt/libc.mdgets an entry: the rule follows a static reading of the console'swcsncat_s(not run on a console),wcscat_sinherits it unread, andmain's narrowstrncat_skeeps a wider rule ([dest, dest + used + length + 1)against[src, src + length + 1), froma998987278), 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 ontomain30ecc1b7b3to add the debt entry (the only conflict was that entry's neighbour indocs/dev/debt/libc.md; the code and test are unchanged): with this change'sStrings.cppand test applied tomain9d6adeb550,guest_wide_stringspasses 3 of 3. The runs below are frommain4ec064d29.guest_wide_stringsgains @oneandonlydean's two cases: the non-overlapping copy above (returns 0, leaves"abcbc"), andu"abc\0xyz"withsrc = dest + 4andcount = 3, where the read range starts inside the write range (returns 22 withdest[0]cleared).main'sStrings.cppand the new test, the test aborts (exit0xc0000409). With the first new row removed it passes, so the non-overlapping copy is the rowmainfails.python tools/check_conventions.py --base origin/mainwith this title and body passes.maindf2569d2c5:guest_wide_stringsexits 0 with the change; withmain'sStrings.cppand the PR's tests it aborts (SIGABRT, exit 134)._sfunctions' overlap rules.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
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 ontomaindf2569d2c5(clean merge). I builtguest_wide_string_testsand ran the executable directly (exit code, not throughctest):main, withcore/libs/prx/libc/src/Strings.cppreset tomain): it aborts (SIGABRT, exit code 134).Not tested: macOS, Windows, the rest of the test suite.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T18:15:11Z.
Review: COMMENTED
Checked against main
a6646f8464and the console-module reading ofwcsncat_son #8269:[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.docs/dev/debt/libc.md? It would say that thewcsncat_soverlap rule follows a static reading oflibc.prx(not run on a console) and thatwcscat_sinherits it without its own code being read. The PR doesn't touch the debt file, and the existing_sentry only covers the size limits.strncat_suses a wider rule ([dest, dest+used+length+1)against[src, src+length+1), froma998987278), andGuestStrings.cpp:137pinsstrncat_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.