Skip to content

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

Closed
xXPBSXx wants to merge 1 commit into
boykopovar:mainfrom
xXPBSXx:fix/snprintf-size-overflow
Closed

xXPBSXx wants to merge 1 commit into
boykopovar:mainfrom
xXPBSXx:fix/snprintf-size-overflow

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

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 5f7aa55 + 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: fix(libc): match Windows snprintf overflow behavior #3982 and fix(libc): reject oversized snprintf buffers on Windows #5070 (closed).
    Depends on: none. Unblocks: none. AI-assisted: yes.
    AI-assisted (Claude Code).
    Hinweis: Vorarbeit in fix(libc): reject oversized snprintf buffers on Windows #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

…xceeds INT_MAX

FreeBSD 11.4 vsnprintf checks the size minus the terminator against INT_MAX
before formatting and, when it is larger, sets errno to EOVERFLOW, writes a
terminator to the buffer and returns EOF (lib/libc/stdio/vsnprintf.c:61-67).
The guest formatted into the buffer and returned the length.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

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

@github-actions github-actions Bot added the fix error Closes an issue (Fixes #N) label Oct 20, 2026

@fogydes fogydes left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions Bot added the libc label Oct 20, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix error Closes an issue (Fixes #N) libc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

libc: snprintf and vsnprintf format into the buffer when the size exceeds INT_MAX, FreeBSD 11.4 fails with EOVERFLOW

3 participants