fix(libSceConvertKeycode): look up keyboard types without signed overflow #10968

Merged
MarcZX10 merged 2 commits from upstream-pr/10968 into main 2026-10-26 09:06:40 +00:00
MarcZX10 commented 2026-10-25 11:32:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T11:32:20Z.


What

ImeKeyboardType::LookUp (core/libs/prx/libSceConvertKeycode/ImeKeyboardType.hpp), used by sceConvertKeycodeGetImeKeyboardType, computed keyboardType - kFirstMappedType (16) in std::int32_t. For a keyboard type below INT32_MIN + 16, such as the 0x80000000 the existing guest_convert_keycode case passes, that subtraction overflows, which is undefined behavior. UBSan reports it:

core/libs/prx/libSceConvertKeycode/ImeKeyboardType.hpp:38:24: runtime error: signed integer overflow: -2147483648 - 16 cannot be represented in type 'int'

The index is now computed in 64 bits. Every keyboard type maps to the same IME keyboard type as before, and types outside the table still map to 0.

Two commits: fix, test. The test adds static_assert(ImeKeyboardType::LookUp(INT32_MIN, 0, 0) == 0) and the same for INT32_MAX; LookUp is constexpr, and signed overflow is an error in a constant expression, so with the previous code the test file does not compile (overflow in constant expression). This makes the regression visible without a sanitizer.

Tested

Linux x86-64 (Debian 12, GCC 12). In an -DANYPS5_ENABLE_ASAN=ON build with -fsanitize=undefined and UBSAN_OPTIONS=halt_on_error=1, guest_convert_keycode fails on current main with the report above and passes with the fix, together with the six guest_convert_keycode_title_* tests. Without the fix, guest_convert_keycode_tests stops compiling once the new static_assert lines are added; with it, it compiles. python3 tools/check_conventions.py --base upstream/main --head HEAD and git diff --check upstream/main pass. Windows, macOS and hardware were 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 under Silent stubs in their library's technical debt file
  • 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 (not applicable; no third-party code added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5004332490 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10968). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T11:32:20Z. --- ### What `ImeKeyboardType::LookUp` (`core/libs/prx/libSceConvertKeycode/ImeKeyboardType.hpp`), used by `sceConvertKeycodeGetImeKeyboardType`, computed `keyboardType - kFirstMappedType` (16) in `std::int32_t`. For a keyboard type below `INT32_MIN + 16`, such as the `0x80000000` the existing `guest_convert_keycode` case passes, that subtraction overflows, which is undefined behavior. UBSan reports it: ``` core/libs/prx/libSceConvertKeycode/ImeKeyboardType.hpp:38:24: runtime error: signed integer overflow: -2147483648 - 16 cannot be represented in type 'int' ``` The index is now computed in 64 bits. Every keyboard type maps to the same IME keyboard type as before, and types outside the table still map to 0. Two commits: fix, test. The test adds `static_assert(ImeKeyboardType::LookUp(INT32_MIN, 0, 0) == 0)` and the same for `INT32_MAX`; `LookUp` is `constexpr`, and signed overflow is an error in a constant expression, so with the previous code the test file does not compile (`overflow in constant expression`). This makes the regression visible without a sanitizer. ### Tested Linux x86-64 (Debian 12, GCC 12). In an `-DANYPS5_ENABLE_ASAN=ON` build with `-fsanitize=undefined` and `UBSAN_OPTIONS=halt_on_error=1`, `guest_convert_keycode` fails on current `main` with the report above and passes with the fix, together with the six `guest_convert_keycode_title_*` tests. Without the fix, `guest_convert_keycode_tests` stops compiling once the new `static_assert` lines are added; with it, it compiles. `python3 tools/check_conventions.py --base upstream/main --head HEAD` and `git diff --check upstream/main` pass. Windows, macOS and hardware were not run. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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 (not applicable; no third-party code added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!10968
No description provided.