fix(libc): reject snprintf capacities above INT_MAX + 1 #9296

Merged
superhe11 merged 3 commits from upstream-pr/9296 into main 2026-10-22 18:29:28 +00:00
superhe11 commented 2026-10-21 17:39:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by superhe11 on 2026-10-21T17:39:02Z.


What

Fixes #8510. snprintf and vsnprintf currently format into a buffer whose declared capacity exceeds INT_MAX + 1. Reject that capacity before reading formatting arguments: return -1, set guest errno to EOVERFLOW (84), and write only the first terminating NUL. Preserve INT_MAX + 1 as a valid capacity.

Carry @xXPBSXx's offered commit f676d6d17524ca6a1dec5a0dfe66680fc0b5f65c, preserving authorship. The behaviour follows FreeBSD 11.4 snprintf.c and vsnprintf.c. Console verification and the null-buffer deviation are recorded in libc's debt file.

The follow-up extends guest_formatting to check both exports, narrow and guest-wide formatting, INT_MAX + 2 and SIZE_MAX rejection, unchanged %n destinations and buffer-tail bytes, valid capacities 0/1/INT_MAX/INT_MAX + 1, and null-buffer size queries. The va_list wrapper also checks that the supplied list is unchanged.

Related: #9087 changes secure formatting. This change leaves the secure variants untouched; a nonmutating git merge-tree with #9087 succeeds.

Tested

Windows x64, WinLibs MinGW-w64 GCC 15.2.0, CMake/Ninja, Release, BUILD_TESTING=ON, full project configuration, rebased onto main 99844c7ee.

  • Baseline: with the extended test kept and only Formatting.cpp restored to main d34da5d69, guest_formatting fails with Formatting check failed. Restoring the fix makes the same test pass.
  • Full libs target: passed, including NID-patched library artifacts. After the final rebase, rebuilt the library artifacts and test executables again.
  • Final runtime tests: 7/7 passed (patched_libraries, guest_formatting, guest_wide_formatting, the three guest_standard_streams modes, and guest_syslog). Total 2.57 seconds; guest_formatting 0.20 seconds.
  • tools/check_conventions.py --base origin/main and git diff --check: passed.

No console or commercial title was tested; Linux/macOS behaviour was not executed locally.

Overlap review

The documentation-only follow-up moves the snprintf debt entry away from the section-start insertion used by #8937. git merge-tree --write-tree with #8937 succeeds. C++ source and test contents are unchanged; conventions and whitespace checks pass.

Manually reviewed the shared-file diffs of #3965, #4627, #5021, #5303, #6044, #6469, #6879, #7081, #7201, #7447, #7648, #7715, #8150 and #8520. None implements this size check. The other debt edits concern different entries; the shared test changes in #4627, #5021 and #7201 add platform/ABI includes. These are not implementation dependencies.

#6044 reorganizes the tests and deletes the old core/libs/tests/Formatting.cpp path. That is a genuine future modify/delete integration concern: preserve or port CheckSizeLimits and its invocation when combining the two PRs. It does not provide functionality this fix requires, so Depends on remains none. The overlap bot may continue to report shared files it cannot compare until those older branches are rebased.

Checklist

  • Based on current main; no other open PR implements this capacity check
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt (no new code comments)
  • Unimplemented paths throw; no silent stubs added
  • No notes, investigation files or images added to the repository
  • New third-party code is a submodule built from source (no new dependencies)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes #8510
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4955800546 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9296). Originally posted by [superhe11](https://github.com/superhe11) on 2026-10-21T17:39:02Z. --- ### What Fixes #8510. `snprintf` and `vsnprintf` currently format into a buffer whose declared capacity exceeds `INT_MAX + 1`. Reject that capacity before reading formatting arguments: return -1, set guest errno to `EOVERFLOW` (84), and write only the first terminating NUL. Preserve `INT_MAX + 1` as a valid capacity. Carry @xXPBSXx's offered commit `f676d6d17524ca6a1dec5a0dfe66680fc0b5f65c`, preserving authorship. The behaviour follows FreeBSD 11.4 [snprintf.c](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdio/snprintf.c) and [vsnprintf.c](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdio/vsnprintf.c). Console verification and the null-buffer deviation are recorded in libc's debt file. The follow-up extends `guest_formatting` to check both exports, narrow and guest-wide formatting, `INT_MAX + 2` and `SIZE_MAX` rejection, unchanged `%n` destinations and buffer-tail bytes, valid capacities 0/1/`INT_MAX`/`INT_MAX + 1`, and null-buffer size queries. The `va_list` wrapper also checks that the supplied list is unchanged. Related: #9087 changes secure formatting. This change leaves the secure variants untouched; a nonmutating `git merge-tree` with #9087 succeeds. ### Tested Windows x64, WinLibs MinGW-w64 GCC 15.2.0, CMake/Ninja, Release, `BUILD_TESTING=ON`, full project configuration, rebased onto main `99844c7ee`. - Baseline: with the extended test kept and only `Formatting.cpp` restored to main `d34da5d69`, `guest_formatting` fails with `Formatting check failed`. Restoring the fix makes the same test pass. - Full `libs` target: passed, including NID-patched library artifacts. After the final rebase, rebuilt the library artifacts and test executables again. - Final runtime tests: 7/7 passed (`patched_libraries`, `guest_formatting`, `guest_wide_formatting`, the three `guest_standard_streams` modes, and `guest_syslog`). Total 2.57 seconds; `guest_formatting` 0.20 seconds. - `tools/check_conventions.py --base origin/main` and `git diff --check`: passed. No console or commercial title was tested; Linux/macOS behaviour was not executed locally. ### Overlap review The documentation-only follow-up moves the snprintf debt entry away from the section-start insertion used by #8937. `git merge-tree --write-tree` with #8937 succeeds. C++ source and test contents are unchanged; conventions and whitespace checks pass. Manually reviewed the shared-file diffs of #3965, #4627, #5021, #5303, #6044, #6469, #6879, #7081, #7201, #7447, #7648, #7715, #8150 and #8520. None implements this size check. The other debt edits concern different entries; the shared test changes in #4627, #5021 and #7201 add platform/ABI includes. These are not implementation dependencies. #6044 reorganizes the tests and deletes the old `core/libs/tests/Formatting.cpp` path. That is a genuine future modify/delete integration concern: preserve or port `CheckSizeLimits` and its invocation when combining the two PRs. It does not provide functionality this fix requires, so `Depends on` remains none. The overlap bot may continue to report shared files it cannot compare until those older branches are rebased. ### Checklist - [x] Based on current `main`; no other open PR implements this capacity check - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt (no new code comments) - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation files or images added to the repository - [x] New third-party code is a submodule built from source (no new dependencies) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes #8510 - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
xXPBSXx commented 2026-10-22 04:11:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-22T04:11:30Z.


Linux data point for 595dcacdf2 (WSL2, Ubuntu 24.04, kernel 6.18 - a virtualised kernel, not bare-metal Linux; GCC 13.3, Release, BUILD_TESTING=ON), this head merged onto main df2569d2c5 (clean merge). I built guest_formatting_tests and ran the executable directly (exit code, not through ctest):

  • With the PR: exit code 0.
  • Without it (the PR's test sources on top of main, with core/libs/prx/libc/src/Formatting.cpp reset to main): it aborts (SIGABRT, exit code 134).
    Not tested: macOS, Windows, the rest of the test suite.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6332491614 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9296#issuecomment-6332491614). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-22T04:11:30Z. --- Linux data point for `595dcacdf2` (WSL2, Ubuntu 24.04, kernel 6.18 - a virtualised kernel, not bare-metal Linux; GCC 13.3, Release, `BUILD_TESTING=ON`), this head merged onto `main` `df2569d2c5` (clean merge). I built `guest_formatting_tests` and ran the executable directly (exit code, not through `ctest`): - With the PR: exit code 0. - Without it (the PR's test sources on top of `main`, with `core/libs/prx/libc/src/Formatting.cpp` reset to `main`): it aborts (SIGABRT, exit code 134). Not tested: macOS, Windows, the rest of the test suite. AI-assisted (Claude Code).
harisibrar1 commented 2026-10-22 18:15:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T18:15:15Z.

Review: APPROVED


Checked against main a6646f8464 and FreeBSD 11.4 lib/libc/stdio/snprintf.c / vsnprintf.c:

  • FreeBSD decrements a nonzero n and then rejects n > INT_MAX with EOVERFLOW, *str = '\0' and EOF. So INT_MAX + 1 is accepted and INT_MAX + 2 and up are rejected, with no clamping and no formatting. SizeOverflowsInt matches that boundary, the errno value (84) and the single NUL write. The null-buffer guard is the one deviation, and the debt entry documents it.
  • The check runs before va_start, so %n targets are never touched, as on FreeBSD.
  • Main still has no size check in snprintf or vsnprintf, and this head merges with main cleanly.
  • Minor: the new debt bullet is separated from the list above it by a blank line, so it renders as its own list.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5618821444 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9296#pullrequestreview-5618821444). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T18:15:15Z. **Review:** APPROVED --- Checked against main a6646f8464 and FreeBSD 11.4 `lib/libc/stdio/snprintf.c` / `vsnprintf.c`: - FreeBSD decrements a nonzero `n` and then rejects `n > INT_MAX` with `EOVERFLOW`, `*str = '\0'` and `EOF`. So `INT_MAX + 1` is accepted and `INT_MAX + 2` and up are rejected, with no clamping and no formatting. `SizeOverflowsInt` matches that boundary, the errno value (84) and the single NUL write. The null-buffer guard is the one deviation, and the debt entry documents it. - The check runs before `va_start`, so `%n` targets are never touched, as on FreeBSD. - Main still has no size check in `snprintf` or `vsnprintf`, and this head merges with main cleanly. - Minor: the new debt bullet is separated from the list above it by a blank line, so it renders as its own list. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9296
No description provided.