fix(libc): fail snprintf and vsnprintf with EOVERFLOW when the size exceeds INT_MAX #8511

Closed
xXPBSXx wants to merge 1 commit from upstream-pr/8511 into main
xXPBSXx commented 2026-10-20 14:22:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T14:22:10Z.


What

snprintf and vsnprintf formatted into the buffer for any size and returned the length. FreeBSD 11.4 vsnprintf rejects a size whose value minus one exceeds INT_MAX: it sets errno to EOVERFLOW (84), stores a NUL in the buffer and returns -1 (lib/libc/stdio/vsnprintf.c:61-67). Both exports now do the same before formatting, on every host build; sizes up to INT_MAX + 1 and size 0 are unchanged.
Fixes #8510
git merge-tree against upstream/main is clean. pr_overlap was not run. No title is known to reach this; the change rests on the FreeBSD source line and a failing test.

Tested

Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main 5f7aa5570c + this commit:

  • guest_formatting: fails (Formatting check failed) with Formatting.cpp taken back to main, passes with the change (3 of 3 runs). New cases: snprintf and vsnprintf with INT_MAX + 2 and SIZE_MAX return -1 with EOVERFLOW and an empty buffer; INT_MAX + 1 still formats.
  • guest_syslog (the only other caller of vsnprintf, with small sizes) passes.
  • python tools/check_conventions.py --base upstream/main: passes.
  • Not tested: Linux (the new check is shared code, not built there), a console, a title run; no title is known to pass such a size. Earlier attempts: #3982 and #5070 (closed).
    Depends on: none. Unblocks: none. AI-assisted: yes.
    AI-assisted (Claude Code).
    Hinweis: Vorarbeit in #5070 (MarcZX10, geschlossen; xXPBSXx hatte dort bereits die Quellzeile bestaetigt). Dieser Branch ist ein eigener Neuaufbau aus der Quelle, kein uebernommener Code.

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; no silent stubs added
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none added)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4937184294 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8511). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-20T14:22:10Z. --- ### What `snprintf` and `vsnprintf` formatted into the buffer for any size and returned the length. FreeBSD 11.4 `vsnprintf` rejects a size whose value minus one exceeds `INT_MAX`: it sets `errno` to `EOVERFLOW` (84), stores a NUL in the buffer and returns -1 (lib/libc/stdio/vsnprintf.c:61-67). Both exports now do the same before formatting, on every host build; sizes up to `INT_MAX + 1` and size 0 are unchanged. Fixes #8510 `git merge-tree` against `upstream/main` is clean. `pr_overlap` was not run. No title is known to reach this; the change rests on the FreeBSD source line and a failing test. ### Tested Windows 10 (19045), MinGW-w64 GCC 15.2.0, Release, on upstream/main 5f7aa5570c + this commit: - `guest_formatting`: fails (`Formatting check failed`) with `Formatting.cpp` taken back to main, passes with the change (3 of 3 runs). New cases: `snprintf` and `vsnprintf` with `INT_MAX + 2` and `SIZE_MAX` return -1 with `EOVERFLOW` and an empty buffer; `INT_MAX + 1` still formats. - `guest_syslog` (the only other caller of `vsnprintf`, with small sizes) passes. - `python tools/check_conventions.py --base upstream/main`: passes. - Not tested: Linux (the new check is shared code, not built there), a console, a title run; no title is known to pass such a size. Earlier attempts: #3982 and #5070 (closed). Depends on: none. Unblocks: none. AI-assisted: yes. AI-assisted (Claude Code). Hinweis: Vorarbeit in #5070 (MarcZX10, geschlossen; xXPBSXx hatte dort bereits die Quellzeile bestaetigt). Dieser Branch ist ein eigener Neuaufbau aus der Quelle, kein uebernommener Code. ### 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; no silent stubs added - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none added) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-20 14:32:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T14:32:45Z.


Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 core/libs/tests/Formatting.cpp
PR 6044 core/libs/tests/Formatting.cpp
PR 7201 core/libs/tests/Formatting.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6293322376 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8511#issuecomment-6293322376). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T14:32:45Z. --- <!-- pr-overlap --> Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 5021](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/tests/Formatting.cpp` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/Formatting.cpp` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/tests/Formatting.cpp` |
fogydes commented 2026-10-20 15:00:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fogydes on 2026-10-20T15:00:34Z.

Review: COMMENTED


Reviewed the diff, verified FreeBSD 11.4 behavior, and built/ran the test suite locally.

Implementation: The helper SizeOverflowsInt(char* buffer, size_t size) checks if (size == 0 || size - 1 <= static_cast<size_t>(INT_MAX)) return false;. For oversized buffers, it sets errno = 84 (EOVERFLOW), places a null terminator in *buffer if non-null, and returns true, triggering -1 in both snprintf_nid_postfix and vsnprintf_nid_postfix.

  • Size 0 correctly returns false (allowing format query calls like snprintf(nullptr, 0, ...) to succeed and calculate length without error).
  • Exact boundary INT_MAX + 1 is preserved (size - 1 <= INT_MAX remains true).
  • This matches FreeBSD 11.4 lib/libc/stdio/vsnprintf.c:61-67 behavior.

Tests: Adds assertions covering INT_MAX + 2 and SIZE_MAX verifying both return -1, set errno = 84, and terminate output[0] == 0. Also asserts INT_MAX + 1 succeeds and formats correctly.

Local verification:

  • Built guest_formatting_tests on Windows 11 with MinGW-w64 GCC 15.2.0 Release.
  • Ran ctest --test-dir build-libs -R "^guest_formatting$" -V: Passed (0.19s, 10000 iterations).
  • CI is all green across Linux, Windows, macOS, and relinker matrix.

No issues found. Clean, standards-compliant fix with precise boundary checks.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5586960619 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8511#pullrequestreview-5586960619). Originally posted by [fogydes](https://github.com/fogydes) on 2026-10-20T15:00:34Z. **Review:** COMMENTED --- Reviewed the diff, verified FreeBSD 11.4 behavior, and built/ran the test suite locally. **Implementation**: The helper `SizeOverflowsInt(char* buffer, size_t size)` checks `if (size == 0 || size - 1 <= static_cast<size_t>(INT_MAX)) return false;`. For oversized buffers, it sets `errno = 84` (`EOVERFLOW`), places a null terminator in `*buffer` if non-null, and returns `true`, triggering `-1` in both `snprintf_nid_postfix` and `vsnprintf_nid_postfix`. - Size 0 correctly returns `false` (allowing format query calls like `snprintf(nullptr, 0, ...)` to succeed and calculate length without error). - Exact boundary `INT_MAX + 1` is preserved (`size - 1 <= INT_MAX` remains true). - This matches FreeBSD 11.4 `lib/libc/stdio/vsnprintf.c:61-67` behavior. **Tests**: Adds assertions covering `INT_MAX + 2` and `SIZE_MAX` verifying both return `-1`, set `errno = 84`, and terminate `output[0] == 0`. Also asserts `INT_MAX + 1` succeeds and formats correctly. **Local verification**: - Built `guest_formatting_tests` on Windows 11 with MinGW-w64 GCC 15.2.0 Release. - Ran `ctest --test-dir build-libs -R "^guest_formatting$" -V`: **Passed** (0.19s, 10000 iterations). - CI is all green across Linux, Windows, macOS, and relinker matrix. No issues found. Clean, standards-compliant fix with precise boundary checks.
oneandonlydean commented 2026-10-20 17:57:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T17:57:15Z.


Thanks for this. On the console, snprintf and vsnprintf don't check the size against INT_MAX: the size is passed straight through to the formatter, so a size above INT_MAX doesn't produce EOVERFLOW there (the same behaviour came up on #3982). Since we aim to match the console, this change would introduce a difference rather than fix one, so I'm closing it. Thank you for looking into it.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6297551679 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8511#issuecomment-6297551679). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T17:57:15Z. --- Thanks for this. On the console, snprintf and vsnprintf don't check the size against INT_MAX: the size is passed straight through to the formatter, so a size above INT_MAX doesn't produce EOVERFLOW there (the same behaviour came up on #3982). Since we aim to match the console, this change would introduce a difference rather than fix one, so I'm closing it. Thank you for looking into it.

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