fix(libc): reject snprintf capacities above INT_MAX + 1 #9296
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9296"
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 superhe11 on 2026-10-21T17:39:02Z.
What
Fixes #8510.
snprintfandvsnprintfcurrently format into a buffer whose declared capacity exceedsINT_MAX + 1. Reject that capacity before reading formatting arguments: return -1, set guest errno toEOVERFLOW(84), and write only the first terminating NUL. PreserveINT_MAX + 1as 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_formattingto check both exports, narrow and guest-wide formatting,INT_MAX + 2andSIZE_MAXrejection, unchanged%ndestinations and buffer-tail bytes, valid capacities 0/1/INT_MAX/INT_MAX + 1, and null-buffer size queries. Theva_listwrapper also checks that the supplied list is unchanged.Related: #9087 changes secure formatting. This change leaves the secure variants untouched; a nonmutating
git merge-treewith #9087 succeeds.Tested
Windows x64, WinLibs MinGW-w64 GCC 15.2.0, CMake/Ninja, Release,
BUILD_TESTING=ON, full project configuration, rebased onto main99844c7ee.Formatting.cpprestored to maind34da5d69,guest_formattingfails withFormatting check failed. Restoring the fix makes the same test pass.libstarget: passed, including NID-patched library artifacts. After the final rebase, rebuilt the library artifacts and test executables again.patched_libraries,guest_formatting,guest_wide_formatting, the threeguest_standard_streamsmodes, andguest_syslog). Total 2.57 seconds;guest_formatting0.20 seconds.tools/check_conventions.py --base origin/mainandgit 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-treewith #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.cpppath. That is a genuine future modify/delete integration concern: preserve or portCheckSizeLimitsand its invocation when combining the two PRs. It does not provide functionality this fix requires, soDepends onremains none. The overlap bot may continue to report shared files it cannot compare until those older branches are rebased.Checklist
main; no other open PR implements this capacity checkImported 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 ontomaindf2569d2c5(clean merge). I builtguest_formatting_testsand ran the executable directly (exit code, not throughctest):main, withcore/libs/prx/libc/src/Formatting.cppreset tomain): it aborts (SIGABRT, exit code 134).Not tested: macOS, Windows, the rest of the test suite.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T18:15:15Z.
Review: APPROVED
Checked against main
a6646f8464and FreeBSD 11.4lib/libc/stdio/snprintf.c/vsnprintf.c:nand then rejectsn > INT_MAXwithEOVERFLOW,*str = '\0'andEOF. SoINT_MAX + 1is accepted andINT_MAX + 2and up are rejected, with no clamping and no formatting.SizeOverflowsIntmatches 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.va_start, so%ntargets are never touched, as on FreeBSD.snprintforvsnprintf, and this head merges with main cleanly.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.