fix(libc): return 0 from the narrow ctype functions for values outside 0..255 #2698

Closed
husseinkad wants to merge 2 commits from upstream-pr/2698 into main
husseinkad commented 2026-10-10 11:19:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by husseinkad on 2026-10-10T11:19:00Z.


What

isalpha, isdigit, isalnum, isspace, isupper, islower, isblank, iscntrl, isprint, isgraph, ispunct, isxdigit, toupper and tolower threw std::invalid_argument for any argument outside EOF and 0..255, which terminates the process. A guest that passes a sign-extended char (for example isalpha(-23) for a byte 0xE9) aborted.

FreeBSD's libc range-checks instead: the classifiers go through __sbmaskrune / __isctype in include/_ctype.h (releng/11.4, main identical), which return 0 for c < 0 or c >= __mb_sb_limit, and toupper/tolower (__sbtoupper/__sbtolower) return c unchanged outside that range; the wide ___runetype/___toupper/___tolower in runetype.c give the same results. The guest isw* functions in RuneLocale.cpp already behave this way. The narrow functions now return 0 (classifiers) or the argument (toupper, tolower) for any value outside 0..255, including EOF. Results for 0..255 are unchanged. The assumption that the console's libc behaves like FreeBSD's here is recorded in TechnicalDebt.

Tested

Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release:

  • guest_character gains -2, -23, -128, -129, 256, 257, 0x1e9, 0xffff, 0x10000, INT_MAX and INT_MIN for all 14 functions; it fails without the change (uncaught exception, exit 0xc0000409) and passes with it. ctest -R "guest_character|guest_locale|guest_rune" passes.
  • Outside the repo: all 14 exports over every int in [-70000, 70000] plus INT_MIN, INT_MIN+1, INT_MAX and INT_MAX-1 (1,960,070 function/value pairs): 0 divergences from a reference of FreeBSD's C-locale behaviour, and 0 differences inside 0..255 and EOF against main; results are unchanged under host-locale and guest setlocale changes and with 8 threads.
  • tools/check_conventions.py --base origin/main passes; tools/pr_overlap.py --branch reports no overlap.
  • Linux not run.

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
  • 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:4810802327 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2698). Originally posted by [husseinkad](https://github.com/husseinkad) on 2026-10-10T11:19:00Z. --- ### What `isalpha`, `isdigit`, `isalnum`, `isspace`, `isupper`, `islower`, `isblank`, `iscntrl`, `isprint`, `isgraph`, `ispunct`, `isxdigit`, `toupper` and `tolower` threw `std::invalid_argument` for any argument outside EOF and 0..255, which terminates the process. A guest that passes a sign-extended `char` (for example `isalpha(-23)` for a byte 0xE9) aborted. FreeBSD's libc range-checks instead: the classifiers go through `__sbmaskrune` / `__isctype` in `include/_ctype.h` (releng/11.4, `main` identical), which return 0 for `c < 0` or `c >= __mb_sb_limit`, and `toupper`/`tolower` (`__sbtoupper`/`__sbtolower`) return `c` unchanged outside that range; the wide `___runetype`/`___toupper`/`___tolower` in `runetype.c` give the same results. The guest `isw*` functions in `RuneLocale.cpp` already behave this way. The narrow functions now return 0 (classifiers) or the argument (`toupper`, `tolower`) for any value outside 0..255, including EOF. Results for 0..255 are unchanged. The assumption that the console's libc behaves like FreeBSD's here is recorded in TechnicalDebt. ### Tested Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release: - `guest_character` gains -2, -23, -128, -129, 256, 257, 0x1e9, 0xffff, 0x10000, INT_MAX and INT_MIN for all 14 functions; it fails without the change (uncaught exception, exit `0xc0000409`) and passes with it. `ctest -R "guest_character|guest_locale|guest_rune"` passes. - Outside the repo: all 14 exports over every int in [-70000, 70000] plus INT_MIN, INT_MIN+1, INT_MAX and INT_MAX-1 (1,960,070 function/value pairs): 0 divergences from a reference of FreeBSD's C-locale behaviour, and 0 differences inside 0..255 and EOF against `main`; results are unchanged under host-locale and guest `setlocale` changes and with 8 threads. - `tools/check_conventions.py --base origin/main` passes; `tools/pr_overlap.py --branch` reports no overlap. - Linux not run. ### 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 - [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
github-actions[bot] commented 2026-10-10 11:19:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T11:19:11Z.


Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6096945895 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2698#issuecomment-6096945895). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T11:19:11Z. --- Thank you for your interest in the project! We are sorry, but we cannot accept more PRs while we work through the existing backlog. This PR has been automatically closed and labeled "intake paused". This is not a judgment of the quality of your changes.

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!2698
No description provided.