fix(libc): fail snprintf and vsnprintf with EOVERFLOW when the size exceeds INT_MAX #8511
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8511"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-20T14:22:10Z.
What
snprintfandvsnprintfformatted into the buffer for any size and returned the length. FreeBSD 11.4vsnprintfrejects a size whose value minus one exceedsINT_MAX: it setserrnotoEOVERFLOW(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 toINT_MAX + 1and size 0 are unchanged.Fixes #8510
git merge-treeagainstupstream/mainis clean.pr_overlapwas 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) withFormatting.cpptaken back to main, passes with the change (3 of 3 runs). New cases:snprintfandvsnprintfwithINT_MAX + 2andSIZE_MAXreturn -1 withEOVERFLOWand an empty buffer;INT_MAX + 1still formats.guest_syslog(the only other caller ofvsnprintf, with small sizes) passes.python tools/check_conventions.py --base upstream/main: passes.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
main; no other open PR implements the same functions.mdfiles or images added to the repositoryAI-assisted (Claude Code).
🤖 Generated with Claude Code
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.
core/libs/tests/Formatting.cppcore/libs/tests/Formatting.cppcore/libs/tests/Formatting.cppImported 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)checksif (size == 0 || size - 1 <= static_cast<size_t>(INT_MAX)) return false;. For oversized buffers, it setserrno = 84(EOVERFLOW), places a null terminator in*bufferif non-null, and returnstrue, triggering-1in bothsnprintf_nid_postfixandvsnprintf_nid_postfix.false(allowing format query calls likesnprintf(nullptr, 0, ...)to succeed and calculate length without error).INT_MAX + 1is preserved (size - 1 <= INT_MAXremains true).lib/libc/stdio/vsnprintf.c:61-67behavior.Tests: Adds assertions covering
INT_MAX + 2andSIZE_MAXverifying both return-1, seterrno = 84, and terminateoutput[0] == 0. Also assertsINT_MAX + 1succeeds and formats correctly.Local verification:
guest_formatting_testson Windows 11 with MinGW-w64 GCC 15.2.0 Release.ctest --test-dir build-libs -R "^guest_formatting$" -V: Passed (0.19s, 10000 iterations).No issues found. Clean, standards-compliant fix with precise boundary checks.
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.
Pull request closed