fix(libc): safely probe the outputs of wctomb_s and wcrtomb_s #20525
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/20525"
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 milescoviello on 2026-11-15T08:17:19Z.
What
wctomb_sandwcrtomb_swrote the converted bytes (viamemcpy, or by passing the guest buffer straight towctomb/wcrtomb), the terminator on their error paths, and the status or result count, all straight through the guest pointers. So a non-null buffer, status or result pointing at unmapped memory faulted the host.Both now convert into a host
MB_LEN_MAXbuffer and copy the bytes out withGuestCopyOut_nid_no_patch, then the status or result. Every other store goes through the same helper:wcrtomb_s's(size_t)-1result and terminator on its error paths.wctomb_s's state-dependence query, which still passes a null buffer towctomb.An unwritable buffer, status or result is reported as the "bad argument" runtime-constraint violation (EINVAL, via the constraint handler) that these functions already raise for a null status, result or state.
wcrtomb_salso stores the failed result first when the result is writable. The conversions, the buffer-too-small (ERANGE) andEILSEQresults, and the bytes written for valid pointers are unchanged. Thewcrtomb_sstate is still read in place.The debt entry goes in a fragment under
docs/dev/debt/changes/and records this as a host-safety fallback; console behaviour there is not measured.Tested
Linux (Ubuntu 26.04.1), GCC 15.2, Release.
guest_bounds_checking(CheckWideToMultibyte), with the test's recording constraint handler: these return 22 and raise one"<function>: bad argument"violation with error 22:wctomb_swith an unmapped status (0x10);wctomb_swith an unmapped buffer (the status is left at its old value);wcrtomb_swith an unmapped result;wcrtomb_swith an unmapped buffer (the result becomes(size_t)-1).BoundsChecking.cppfrommainand these tests,guest_bounds_checkingsegfaults.mainin this file and gets no new conflict from this change.tools/check_conventions.pypasses on the branch and this body;tools/debt_fold.py --checkpasses with the fragment applied tomain.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)Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-15T08:37:11Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
953e872ed3:tools/check_conventions.pypassed;git diff --checkclean.guest_bounds_checking_testsbuilt cleanly.guest_bounds_checking):core/libs/prx/libc/src/BoundsChecking.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me