Skip to content

fix(libc): return WEOF for out-of-range btowc arguments - #4956

Closed
Flamingo572 wants to merge 1 commit into
boykopovar:mainfrom
Flamingo572:fix/libc-btowc-out-of-range
Closed

Flamingo572 wants to merge 1 commit into
boykopovar:mainfrom
Flamingo572:fix/libc-btowc-out-of-range

Conversation

@Flamingo572

Copy link
Copy Markdown
Contributor

What

In libc, btowc (btowc_nid_postfix) only checked c == -1 before casting c to unsigned char. Values outside the valid byte range (such as 256, 0x100, 0x20ac, -129, or -1000) silently truncated into arbitrary character values (e.g. btowc(256) returned 0 / wide null) instead of returning WEOF (0xffffffff), as required by ISO C11 7.29.5.1 and FreeBSD btowc(3).
btowc_nid_postfix now returns GuestRune::Weof for any argument where c == -1 || c < -128 || c > 255. Added boundary checks to GuestRuneLocale.cpp mirroring the existing out-of-range assertions for wctob_nid_postfix.

Tested

Linux x86-64 (GCC 15.2 C++20). Verified guest_rune_locale_tests passes with the change and fails on main without it. Verified python3 tools/check_conventions.py --base origin/main (0 findings).

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

@xXPBSXx

xXPBSXx commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Windows test of this PR.

Environment: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, BUILD_TESTING=ON, -j2, libusb-cmake from a local checkout. Built from main ea2bbefb2 with 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).

@CnRJay CnRJay left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants