fix(libc): match Windows snprintf overflow behavior #3982
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3982"
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 MarcZX10 on 2026-10-11T20:34:06Z.
What
snprintf/vsnprintfoverflow behavior: sizes aboveINT_MAX + 1clear the output, setEOVERFLOW, and return-1.INT_MAX,INT_MAX + 1,INT_MAX + 2,SIZE_MAX, null buffers, and preserved errno behavior.*str = '\0'unconditionally. This is deliberate hardening for that error path, not a general claim that null buffers are accepted.Tested
guest_formatting_tests; CTestguest_formattingpassed (1/1). This does not exercise the Windows-only branch.tools/check_conventions.py --base upstream/mainandgit diff --checkpassed.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repositoryImported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-11T22:56:37Z.
Windows run (Windows 11 26300, WinLibs GCC 15.2.0 r7, Release, full build):
0a8b1d32b:guest_formatting,guest_wide_formatting,guest_standard_streams,file_streamsandguest_crtpass 3/3. WithFormatting.cppreverted,guest_formattingaborts (Formatting check failed), so the new size checks catch the bug on Windows.main(ae7dbfa9c, plus #4021 so that Windows builds):guest_formattingpasses 3/3.guest_formattingfails. The cause is #3987's test, which still expects%1$dand%'dto throw (details there), not this change.Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-11T23:32:49Z.
Review: COMMENTED
Verified against the reference: the boundary is FreeBSD's, exactly. The honest coverage picture is that nothing on this PR builds or runs it — and one sentence of the debt entry is not true for the wide-format route.
Head
d5f16993, merged onto my checkout'supstream/mainat752db7da— 145 commits ahead of this PR's own GitHub base0a8b1d32;Formatting.cppis unchanged between the two, the test file andTechnicalDebt.mdmoved and both merged cleanly. The API reportsMERGEABLE/CLEAN. This is the fix for #3974, whose own caveat — "Windows only; the Linux glibc path is not covered and was not built" — is the right frame for everything below.The rule is right, checked against the source rather than the issue
FreeBSD 11.4
lib/libc/stdio/vsnprintf.c,vsnprintf_l:It decrements before comparing, so the overflow point is
size > INT_MAX + 1— which is whatsize - 1 <= INT_MAXexpresses — and the actions line up (*str = '\0',errno = EOVERFLOW,return -1=EOF). I ran both predicates against each other over0..199,INT_MAX-3 … INT_MAX+5, and the extremes (2^31,2^31+1/2/3,2^32-1,2^63,2^64-1): no mismatch on any input,INT_MAX + 1allowed andINT_MAX + 2refused, which is the pair your second commit's headline names. Thesize == 0disjunct comes first sosize - 1never underflows — and it is load-bearing, since without itSIZE_MAX <= INT_MAXis false andsize == 0would read as an overflow.The errno is the guest's, and 84 is this tree's established form for it
__error_nid_postfix()returns&errno(core/libs/prx/libc/src/Process.cpp:158-160), so the bareerrnohere is the guest's slot, and the repo already maps the host'sEOVERFLOWonto the guest's 84 (core/libs/prx/libc/src/FileAndHeap.cpp:215and:226,core/libs/prx/libkernel/File/src/Open.cpp:129-130) and names that value directly (libkernel/KernelErrors.hpp:91,libkernel/Pthread/Posix/Sem.cpp:21). A host macro would give the guest something other than 84 (75 on Linux, and the MS CRT's own value differs again), so the literal is the deliberate and correct form here.One deliberate divergence worth a clause. FreeBSD writes
*str = '\0'in that branch unconditionally; your helper guards the buffer first, so a guest passing(nullptr, INT_MAX + 2)gets-1where the reference writes through a null pointer. That is a hardening rather than a port, and "as FreeBSD 11.4 does" reads as byte-for-byte — one clause ("ours also accepts a null buffer, where the reference writes through it") removes the puzzle.Finding — the entry's Linux sentence is not true for the wide-format route
The new debt entry says "On Linux the limit is whatever the host glibc applies". That holds only for the narrow path:
snprintf_nid_postfix(core/libs/prx/libc/src/Formatting.cpp:311-317) andvsnprintf_nid_postfix(:407-419) send any format carrying a wide conversion toLibcDetail::FormatWindows, where glibc is not involved and there is no size check —FormatOutputvalidates the output length, notsize(WindowsFormatting.hpp:161-165, ctor at:64). So a guest callingsnprintf(buf, INT_MAX + 2, "%ls")on Linux is neither refused withEOVERFLOW— which the console, being FreeBSD-derived, does for that format too — nor handled by glibc. Suggested clause: "…whatever the host glibc applies for the narrow formats; a wide conversion goes to the guest formatter instead, so glibc's limit does not apply to it".Coverage, as it actually is — and this is the part I would put in the description
Nothing on this PR builds or runs
guest_formatting:gh pr checks 3982reportslinuxandwindowsas skipping, both gated topush/workflow_dispatchwithfullset only for manual dispatch (build.yml:193,:240), and the one PR-time job,relinker, configures-DANYPS5_RELINKER_ONLY=ON, whereCMakeLists.txt:71-73returns beforecore/libs/prx/libc— the target does not exist in that build at all. What has run the boundary is @LeandroLP23's manual Windows build reported below (3/3 on this base, andguest_formattingaborts withFormatting.cppreverted), plus the predicate comparison above. My local run —ctest --test-dir build -R '^guest_formatting$'→1/1 Passed, andnm -C build/tests/guest_formatting_tests | grep -c ExceedsIntMax→0— is a non-regression result only, because the new checks sit inside#ifdef _WIN32(core/libs/tests/Formatting.cpp:136-158).Also checked, and it holds up
snprintfdirectly (Formatting.cpp:277, asserted at test:150,:155,:157) andvsnprintfthroughFormatList(test :153), whoseSIZE_MAXprobe exercises the second entry point.sprintf/vsprintfhave no size argument to guard, and the entry documentssnprintf_s/vsnprintf_sas unlimited — so the scope statement is honest rather than an omission.gh pr view 3982 --json commits).%1$d/%'dbehaviour, not from this change.Not verified, so it is not claimed: the console's own
libc.prx— as #3974 says, the code is assumed shared with FreeBSD and unchecked on a title — and Windows execution by me; the Windows evidence in this thread is @LeandroLP23's manual build, not mine.Hermes Agent 🤖 at your service
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T08:24:50Z.
Conflicts with main in
core/libs/tests/Formatting.cpp(after #3978); please rebase. While there,ExceedsIntMaxdoesn't need the#ifdef _WIN32: on Linux a format with a wide conversion already goes toFormatWindowswith no size check, and with the check on both hosts the Linux sentence in the TechnicalDebt entry can go.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T19:43:40Z.
Thanks for the source-level review and boundary comparison. I agree the debt wording should distinguish the narrow Linux path from the wide-conversion path, and the description should call out the deliberate null-buffer hardening difference from FreeBSD. The Windows run reported above confirms the focused boundary test catches the regression on the PR code; the Linux run remains a non-regression check only. I will update the wording to make those limits explicit.
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T20:04:06Z.
Thanks — the rebase reads clean from here too. Head
e0ca58e5, force-pushed 18:47:56Z ontomain1acfa7f7(main has since moved 21 commits, to3e0e01f3, and the API still reportsMERGEABLE/CLEAN), so the conflict incore/libs/tests/Formatting.cpp@oneandonlydean flagged is gone. Every PR-time check is green —conventions,eligibility,relinkeron ubuntu/windows/macos,write-watch— withlinux/windowsstill skipping, since full builds are dispatch-only.One thing that carries across the force-push:
ExceedsIntMaxis byte-identical between the head I reviewed (d5f16993) ande0ca58e5, so the boundary comparison still describes the current code. Re-ran it against FreeBSD 11.4's decrement-then-compare over 213 inputs (0..199,INT_MAX-3 … INT_MAX+5,1<<31+0..3,0xffffffff,1<<63,SIZE_MAX): 0 mismatches; largest size allowed2147483648(INT_MAX + 1), smallest refused2147483649(INT_MAX + 2).For the record the two wording edits are two different edits, and the second is @oneandonlydean's from the rebase comment:
docs/dev/debt/libc.mdstill says "…On Linux the limit is whatever the host glibc applies…". Splitting narrow from wide is the right fix: on Linuxsnprintf_nid_postfix(Formatting.cpp:363-366) hands any format carrying a wide conversion toLibcDetail::FormatWindows, where glibc is not in the picture and no size check runs — so glibc's limit does not govern that route.#ifdef(oneandonlydean's). The guard is still Windows-only throughout: the helper atFormatting.cpp:34-41, the Windowssnprintf_nid_postfixat:328(inside the block opened at:310),vsnprintf_nid_postfix's Windows arm at:463, and the checks attests/Formatting.cpp:179-201. If you take their suggestion — check before the wide-format dispatch on both hosts — the Linux sentence becomes deletable rather than narrowable, and the two asks collapse into one. Keeping #3974's "Windows only" scope is also defensible, but then the entry's Linux sentence and the#ifdefhave to be made to agree.Nothing else is outstanding from my side; the null-buffer hardening clause in the description is the only other wording point.
Hermes Agent 🤖 at your service
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-12T20:31:16Z.
I updated the debt entry to scope the Linux glibc limit to narrow formats and note the guest formatter path for wide conversions (
e3bf9429). The description now also states the null-buffer behavior specifically for the oversized-size error path and distinguishes it from FreeBSD. I left the implementation unchanged; the broader Linux guard suggestion would change behavior and needs separate evidence.Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T20:44:29Z.
Both updates read as described, and I would keep the implementation where it is — here is the measurement behind that call.
Read at
e3bf9429. The debt entry (docs/dev/debt/libc.md) now splits the two Linux routes and the code agrees:snprintf_nid_postfix(core/libs/prx/libc/src/Formatting.cpp:363-369) hands a format carrying a guest wide conversion toLibcDetail::FormatWindowsand everything else tostd::vsnprintf, andvsnprintf_nid_postfixsplits the same way on its Linux arm (:466-473). On the guest-formatter route the only length guard is on the output, not onsize(WindowsFormatting.hpp:95-98constructor,:100-102inAppend). The description clause is narrowed to the right spot as well:ExceedsIntMax(Formatting.cpp:35-40) testsif (buffer)before it clears, where FreeBSD 11.4 writes*str = '\0'unconditionally.Enabling the guard on Linux is new Linux behaviour, not extra coverage — with the threshold stated precisely.
ExceedsIntMaxreturns false forsize - 1 <= INT_MAX, so it fires fromINT_MAX + 2upward andINT_MAX + 1stays a deliberate no-op (that is the commit this PR carries). On the Linux narrow route the host checks nothing aboutsize. Measured here (CachyOS, glibc 2.44), callingvsnprintf— the functionFormatting.cpp:366calls — with"%d", 1:So a Linux guest gets a normal formatted write-back at each of those sizes today, and with the guard on,
INT_MAX + 2and above would return-1withEOVERFLOW(84) instead. That is a behaviour change on a route #3974 explicitly leaves out — its own caveat is "Windows only; the Linux glibc path is not covered and was not built" — so keeping it out of this PR is right, and it is now a visible separate question rather than one this PR has to answer. (glibc does fail withEOVERFLOWwhen the output would exceedINT_MAX, but that is the output length, notsize.)One consequence for the wording. "Narrow formats retain the host glibc limit" reads as though glibc enforces something on
size, and on this host nothing does, at any size I tested. Neither Linux route applies the check the Windows route now applies: the narrow formats reach the hostvsnprintf, and a format containing a guest wide conversion reaches the guest formatter, which bounds the output length rather thansize. A clause along those lines would record the state as it is — your wording call, and it does not change the merge decision.Everything else from the review stands: the predicate comparison against FreeBSD 11.4's decrement-then-compare, the call-site coverage, and the coverage caveat — the
relinkerjob's configure returns beforecore/libs, soguest_formattingis not a target in that build at all, and the boundary evidence is @LeandroLP23's Windows build plus the predicate differential rather than a PR-time run.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-13T02:42:43Z.
What shows that the console's
snprintfhas this check? Its printf family is Dinkumware's (_Printf), not FreeBSD'svfprintf.c, so the FreeBSD 11.4 comparison doesn't cover it. Keeping the check to Windows also leaves the guest's result depending on the host. With a console result, or a title that passes a size aboveINT_MAX, this can go in on both hosts; otherwise I'll close it.Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-13T03:36:38Z.
You're right that nothing I measured shows the console's
snprintfhas this check. The 213-input comparison is against FreeBSD 11.4's decrement-then-compare, and that is the only reference it covers.You say the console's printf family is Dinkumware's
_Printf, not FreeBSD'svfprintf.c. I haven't verified that, and I haven't checked whether the console'ssnprintfhas this check, so the console-side claim is unverified. Please treat the FreeBSD match as a reference comparison only, not evidence of console behaviour.I'm leaving the implementation as it is until there is evidence either way.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T07:39:51Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/tests/Formatting.cppL5core/libs/tests/Formatting.cppL188docs/dev/debt/libc.mdL22Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T10:13:15Z.
Closing, since the premise doesn't hold on the console.
@oneandonlydean asked what shows that the console's
snprintfhas this check. I disassembledsnprintf(eLdDw6l0-bU) in thelibc.prxshipped by a real title (Astro's Playroom):if (size != 0) size--;and calls_Printfwith that size. There is no comparison againstINT_MAXand noerrnowrite.min(remaining, n)bytes._Printfhas noEOVERFLOW(84) store, and its only0x7fffffffcomparisons are in width/precision parsing.So a size above
INT_MAX + 1is not an error there; the FreeBSD 11.4vsnprintf.ccomparison doesn't apply, because the console's printf family is Dinkumware's. Making this Windows-only would also make the guest result depend on the host.Welcome to reopen with a console result or a title that passes such a size.
Pull request closed