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

Closed
Flamingo572 wants to merge 1 commit from upstream-pr/4956 into main
Flamingo572 commented 2026-10-13 06:24:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Flamingo572 on 2026-10-13T06:24:35Z.


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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4839271728 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4956). Originally posted by [Flamingo572](https://github.com/Flamingo572) on 2026-10-13T06:24:35Z. --- ### 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 - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
xXPBSXx commented 2026-10-13 07:05:45 +00:00 (Migrated from github.com)

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-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).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6140227682 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4956#issuecomment-6140227682). Originally posted by [xXPBSXx](https://github.com/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-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 commented 2026-10-14 01:26:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-10-14T01:26:48Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5512003331 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4956#pullrequestreview-5512003331). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-10-14T01:26:48Z. **Review:** COMMENTED --- 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: ```c 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 commented 2026-10-14 02:35:18 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159615266 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4956#issuecomment-6159615266). Originally posted by [oneandonlydean](https://github.com/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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4956
No description provided.