fix(libc): return WEOF for out-of-range btowc arguments #4956
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4956"
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 Flamingo572 on 2026-10-13T06:24:35Z.
What
In
libc,btowc(btowc_nid_postfix) only checkedc == -1before castingctounsigned char. Values outside the valid byte range (such as256,0x100,0x20ac,-129, or-1000) silently truncated into arbitrary character values (e.g.btowc(256)returned0/ wide null) instead of returningWEOF(0xffffffff), as required by ISO C11 7.29.5.1 and FreeBSDbtowc(3).btowc_nid_postfixnow returnsGuestRune::Weoffor any argument wherec == -1 || c < -128 || c > 255. Added boundary checks toGuestRuneLocale.cppmirroring the existing out-of-range assertions forwctob_nid_postfix.Tested
Linux x86-64 (GCC 15.2 C++20). Verified
guest_rune_locale_testspasses with the change and fails on main without it. Verifiedpython3 tools/check_conventions.py --base origin/main(0 findings).Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-13T07:05:45Z.
Windows test of this PR.
Environment: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release,
BUILD_TESTING=ON,-j2,libusb-cmakefrom a local checkout. Built frommainea2bbefb2with this PR and six other open PRs (#4958, #4956, #4954, #4953, #4946, #4942, #4927) merged together; the merges had no conflict.Result: guest_rune_locale (1 of 1) pass.
Not checked: whether the test fails without the change in the library code; the PR was not tested alone, only in the combined merge (the tests of the other PRs in the batch also pass); no title run; Linux not tested.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T01:26:48Z.
Review: COMMENTED
I think this moves
btowcaway from FreeBSD rather than towards it. FreeBSD'sbtowc_l(lib/libc/locale/btowc.c) doesn't range-check its argument. It returnsWEOFonly forEOF, then converts withcc = (char)cand runs the locale'smbrtowcon that one byte:In the C locale (
_none_mbrtowc, which maps every byte to(unsigned char)of itself), that givesbtowc(256) == 0,btowc(0x20ac) == 0xac,btowc(-129) == 127andbtowc(-1000) == 0x18. That is the truncationmaindoes today withstatic_cast<unsigned char>(c). The PR's own examples show this:btowc(256)returning 0 is the FreeBSD result, not a bug.Passing a value outside the range is undefined in ISO C, so a title that does it gets FreeBSD's truncation on the console. Returning
WEOFwould change what such a title sees. Unless there's a console measurement showingWEOF(the console's libc could in principle differ from FreeBSD here), I'd keepmain's behaviour and turn the new test cases into assertions of the truncated values instead.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:35:18Z.
Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Pull request closed