fix(libc): check the C11 runtime constraints in snprintf_s and vsnprintf_s #6315
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6315"
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 hypermoist on 2026-10-15T11:10:48Z.
What
snprintf_sandvsnprintf_sbehaved as plainsnprintf/vsnprintf: the C11 K.3.5.3.5 and K.3.5.3.12 runtime constraints were not checked, so a null buffer with a nonzero size crashed in the host formatter,size == 0returned the would-be length,%nwrote through its pointer, and a null%sprinted(null). This was recorded indocs/dev/debt/libc.md.Both now follow the same rules as
snwprintf_s/vsnwprintf_s, withRSIZE_MAXtaken asSIZE_MAX >> 1:va_list,sizeof 0 or aboveRSIZE_MAX, any%nconversion, or a null%s/%ls/%Sargument returns a negative value and setsbuffer[0]to zero when the buffer and size allow it;The checks live in the shared formatter (
LibcDetail::FormatWindows), which gets asecureflag that throws a dedicatedFormatConstraintViolation. Only that exception counts as a violation, so an unsupported conversion still throws, as it does for the other printf functions. On Linux both functions now go through this formatter for every format, as they already did on Windows and for wide formats on Linux; previously non-wide formats on Linux went to glibc.The constraint handler is not invoked, as for the wide
_sfunctions. That and the console's exact error value stay in the debt entry. Thevsscanf_spart of the old entry is unchanged and now has its own entry.Tested
CachyOS Linux (kernel 7.2.9), GCC 16.2.1, Release, on
main8856f180c.guest_formattinggets new checks for truncation and the full-length return, an empty format, null buffer and format, size 0 and aboveRSIZE_MAX(buffer untouched),%nand%hhn(pointer untouched), null%s,%lsand positional%S, and an unsupported conversion still throwing. Onmainthese checks crash invsnprintf_swith a null buffer.guest_formatting,guest_wide_formatting,guest_strings.tools/check_conventions.py --base origin/mainpasses.FormatWindowsand only gain the same wrapper.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T11:13:58Z.
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)docs/dev/debt/libc.mdL36-37docs/dev/debt/libc.mdL37-39Some 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/prx/libc/include/WindowsFormatting.hppcore/libs/tests/Formatting.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/tests/Formatting.cppdocs/dev/debt/libc.mdcore/libs/prx/libc/src/Formatting.cppdocs/dev/debt/libc.mddocs/dev/debt/libc.mdcore/libs/prx/libc/include/WindowsFormatting.hppdocs/dev/debt/libc.mdcore/libs/tests/Formatting.cppImported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-16T03:15:32Z.
Rebased onto current
main(5d9dcc42d). The only conflict was indocs/dev/debt/libc.md, where the entries had been reordered: the oldvsnprintf_s and vsscanf_sentry is replaced by thesnprintf_s and vsnprintf_sandvsscanf_sentries as before.guest_formattingandguest_wide_formattingpass andcheck_conventions.pyis clean. #6483 (opened after this PR) changes a line in every printf-family function, so it touchesvsnprintf_sandsnprintf_shere too; whichever lands second will need a small rebase.🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-16T10:42:07Z.
Review: COMMENTED
Tested on Linux: Ubuntu 26.04, GCC 15.2, CMake 4.2.3, Release full build with
BUILD_TESTING=ON, i7-8700. Head40df475a8merged ontomain72c5eed1f.guest_formatting,guest_wide_formattingandguest_stringspass. With only the test applied (main'sWindowsFormatting.hppandFormatting.cpp),guest_formattingsegfaults, so the new checks catch the old behaviour.-E "^agc|gpu|vulkan|video_out"), matches main: 648/650, and the twofile_writer_*failures also happen on main on this host.Since non-wide formats on Linux no longer go to glibc, I compared
snprintf_s(buf, 32, ...)on main and on this PR for 22 formats. 16 give the same output. The other 6:%f %+f % fwith NaN, NaN, -NaNnan +nan -nannan nan nan%pwith null(nil)0x0%Ld5Invalid integer length%qd5Unsupported format conversion '%q'%b101Unsupported format conversion '%b'%5%%Unsupported format conversion '%5%'The first two now follow FreeBSD. The last four throw, as they already do on Windows and in the wide path. Two things follow from this:
docs/dev/debt/libc.mdstill says "Linux retains host formatting for formats without wide arguments or%m, so a narrow%pand a NaN there still follow the host". That is no longer true forsnprintf_sandvsnprintf_s, so the entry could name them as exceptions.snprintfandsnprintf_snow disagree for these formats:snprintfstill goes to glibc and prints(nil)and-nan.Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
Imported from boykopovar/AnyPS5. Originally posted by harmsluis on 2026-10-17T17:45:19Z.
Windows run (Windows 11 build 26300, non-elevated, WinLibs MinGW-w64 GCC 15.2.0 r7, Ninja, Release,
BUILD_TESTING=ON), since the PR lists Windows as not built:main66702f1cf(baseline):guest_formatting,guest_wide_formattingandguest_stringspass 3/3.tests/Formatting.cpponmain:guest_formattingaborts (0xc0000409) with an uncaughtstd::invalid_argument: Null formatting argumentfromFormatWindows, so the new checks reach the Windows formatter and catch the gap there.40df475a8) merged onto66702f1cfwithout conflicts: builds with no new warnings;guest_formatting,guest_wide_formattingandguest_stringspass 5/5 (ctest --repeat until-fail:5), andguest_standard_streams,file_streamsandguest_crtpass 3/3.Not covered: the full build, and any title run.