fix(libc): fail wide printf on a width or precision beyond int #10813

Merged
MarcZX10 merged 2 commits from upstream-pr/10813 into main 2026-10-27 08:14:33 +00:00
MarcZX10 commented 2026-10-25 02:40:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T02:40:42Z.


What

The wide printf family (swprintf, vswprintf, snwprintf_s and the stream variants that share FormatWide) accumulated the digits of a field width or precision in an int with no bound, so a digit string above INT_MAX overflowed a signed integer. A * width of INT_MIN also overflowed when it was negated. A width or precision that does not fit an int now fails the call like any other invalid format (empty buffer, -1), which is also what glibc's swprintf returns for it; widths up to INT_MAX parse as before. Two commits: fix, test.

Open PRs #10766, #10379, #9953 and others touch FormattingWide.cpp for unrelated behaviour; this change only touches the width and precision parsing.

Tested

Rebase follow-up (2026-10-25): rebased onto current upstream main e7e3e9c1984c293f06bff08428b8c89b7b932ac2 without conflicts. On the rebased head, GCC 12.2 C++20 -Wall -Wextra -Werror syntax checks passed for FormattingWide.cpp and GuestWideFormatting.cpp; git diff --check and tools/check_conventions.py --base upstream/main passed. The full local CMake test build could not configure because the SDL2 CMake package is unavailable, so no post-rebase runtime test is claimed. The runtime evidence below is from the prior base.

Linux x86-64 (Debian 12, GCC 12), Ninja RelWithDebInfo, -DANYPS5_ENABLE_ASAN=ON with -fsanitize=undefined. The extended guest_wide_formatting case calls swprintf with %99999999999d, %.99999999999d, %4294967296s, %.4294967296s, %2147483648ls, %.2147483648ls and a * width of INT_MIN, and checks a negative * width still left-justifies. On current main UndefinedBehaviorSanitizer stops it with FormattingWide.cpp:130:72: runtime error: signed integer overflow: 999999999 * 10 cannot be represented in type 'int'; with the fix it passes. The console's result for these formats was not measured. python3 tools/check_conventions.py --base upstream/main --head HEAD and git diff --check upstream/main HEAD 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:5001214207 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10813). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T02:40:42Z. --- ### What The wide printf family (`swprintf`, `vswprintf`, `snwprintf_s` and the stream variants that share `FormatWide`) accumulated the digits of a field width or precision in an `int` with no bound, so a digit string above `INT_MAX` overflowed a signed integer. A `*` width of `INT_MIN` also overflowed when it was negated. A width or precision that does not fit an `int` now fails the call like any other invalid format (empty buffer, -1), which is also what glibc's `swprintf` returns for it; widths up to `INT_MAX` parse as before. Two commits: fix, test. Open PRs #10766, #10379, #9953 and others touch `FormattingWide.cpp` for unrelated behaviour; this change only touches the width and precision parsing. ### Tested Rebase follow-up (2026-10-25): rebased onto current upstream `main` `e7e3e9c1984c293f06bff08428b8c89b7b932ac2` without conflicts. On the rebased head, GCC 12.2 C++20 `-Wall -Wextra -Werror` syntax checks passed for `FormattingWide.cpp` and `GuestWideFormatting.cpp`; `git diff --check` and `tools/check_conventions.py --base upstream/main` passed. The full local CMake test build could not configure because the SDL2 CMake package is unavailable, so no post-rebase runtime test is claimed. The runtime evidence below is from the prior base. Linux x86-64 (Debian 12, GCC 12), Ninja RelWithDebInfo, `-DANYPS5_ENABLE_ASAN=ON` with `-fsanitize=undefined`. The extended `guest_wide_formatting` case calls `swprintf` with `%99999999999d`, `%.99999999999d`, `%4294967296s`, `%.4294967296s`, `%2147483648ls`, `%.2147483648ls` and a `*` width of `INT_MIN`, and checks a negative `*` width still left-justifies. On current `main` UndefinedBehaviorSanitizer stops it with `FormattingWide.cpp:130:72: runtime error: signed integer overflow: 999999999 * 10 cannot be represented in type 'int'`; with the fix it passes. The console's result for these formats was not measured. `python3 tools/check_conventions.py --base upstream/main --head HEAD` and `git diff --check upstream/main HEAD` 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
MarcZX10 commented 2026-10-25 06:03:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T06:03:22Z.

Review: COMMENTED


I noticed the additional Windows report on this PR: swprintf_nid_postfix still stack-overflows for %10000000d, %2147483647d and %.100000000d through the std::snprintf path in AppendNumber, even though those widths/precisions fit in int. That report is from a reviewer’s WinLibs GCC 15.2 run, not an independent reproduction on my side, and the failure also occurs on main; I am not treating it as a regression introduced here. Since this change specifically hardens wide-format width/precision handling, could you assess whether rejecting or otherwise safely handling these very large but representable values belongs in this PR, or track it separately? A regression case at the largest in-range value would make the boundary clear.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5641794509 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10813#pullrequestreview-5641794509). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T06:03:22Z. **Review:** COMMENTED --- I noticed the additional Windows report on this PR: `swprintf_nid_postfix` still stack-overflows for `%10000000d`, `%2147483647d` and `%.100000000d` through the `std::snprintf` path in `AppendNumber`, even though those widths/precisions fit in `int`. That report is from a reviewer’s WinLibs GCC 15.2 run, not an independent reproduction on my side, and the failure also occurs on `main`; I am not treating it as a regression introduced here. Since this change specifically hardens wide-format width/precision handling, could you assess whether rejecting or otherwise safely handling these very large but representable values belongs in this PR, or track it separately? A regression case at the largest in-range value would make the boundary clear.
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!10813
No description provided.