Repository navigation
fix(libc): return WEOF for out-of-range btowc arguments - #4956
Flamingo572 wants to merge 1 commit into
Conversation
|
Windows test of this PR. Environment: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, 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). |
CnRJay
left a comment
There was a problem hiding this comment.
I think this moves btowc away from FreeBSD rather than towards it. FreeBSD's btowc_l (lib/libc/locale/btowc.c) doesn't range-check its argument. It returns WEOF only for EOF, then converts with cc = (char)c and runs the locale's mbrtowc on that one byte:
if (c == EOF)
return (WEOF);
cc = (char)c;
if (XLOCALE_CALLBACKS(l)->__mbrtowc(&wc, &cc, 1, &mbs, l) > 1)
return (WEOF);
return (wc);In the C locale (_none_mbrtowc, which maps every byte to (unsigned char) of itself), that gives btowc(256) == 0, btowc(0x20ac) == 0xac, btowc(-129) == 127 and btowc(-1000) == 0x18. That is the truncation main does today with static_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 WEOF would change what such a title sees. Unless there's a console measurement showing WEOF (the console's libc could in principle differ from FreeBSD here), I'd keep main's behaviour and turn the new test cases into assertions of the truncated values instead.
|
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. |
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)