fix(libc): check the C11 runtime constraints in snprintf_s and vsnprintf_s #6315

Merged
hypermoist merged 1 commit from upstream-pr/6315 into main 2026-10-18 22:17:04 +00:00
hypermoist commented 2026-10-15 11:10:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-15T11:10:48Z.


What

snprintf_s and vsnprintf_s behaved as plain snprintf/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 == 0 returned the would-be length, %n wrote through its pointer, and a null %s printed (null). This was recorded in docs/dev/debt/libc.md.

Both now follow the same rules as snwprintf_s/vsnwprintf_s, with RSIZE_MAX taken as SIZE_MAX >> 1:

  • a null buffer or format, a null va_list, size of 0 or above RSIZE_MAX, any %n conversion, or a null %s/%ls/%S argument returns a negative value and sets buffer[0] to zero when the buffer and size allow it;
  • otherwise the output is truncated to the buffer and the full length is returned.

The checks live in the shared formatter (LibcDetail::FormatWindows), which gets a secure flag that throws a dedicated FormatConstraintViolation. 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 _s functions. That and the console's exact error value stay in the debt entry. The vsscanf_s part 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 main 8856f180c.

  • guest_formatting gets new checks for truncation and the full-length return, an empty format, null buffer and format, size 0 and above RSIZE_MAX (buffer untouched), %n and %hhn (pointer untouched), null %s, %ls and positional %S, and an unsupported conversion still throwing. On main these checks crash in vsnprintf_s with a null buffer.
  • Pass: guest_formatting, guest_wide_formatting, guest_strings.
  • tools/check_conventions.py --base origin/main passes.
  • Windows: not built or tested. The Windows functions already used FormatWindows and only gain the same wrapper.

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 (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4872307632 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6315). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-15T11:10:48Z. --- ### What `snprintf_s` and `vsnprintf_s` behaved as plain `snprintf`/`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 == 0` returned the would-be length, `%n` wrote through its pointer, and a null `%s` printed `(null)`. This was recorded in `docs/dev/debt/libc.md`. Both now follow the same rules as `snwprintf_s`/`vsnwprintf_s`, with `RSIZE_MAX` taken as `SIZE_MAX >> 1`: - a null buffer or format, a null `va_list`, `size` of 0 or above `RSIZE_MAX`, any `%n` conversion, or a null `%s`/`%ls`/`%S` argument returns a negative value and sets `buffer[0]` to zero when the buffer and size allow it; - otherwise the output is truncated to the buffer and the full length is returned. The checks live in the shared formatter (`LibcDetail::FormatWindows`), which gets a `secure` flag that throws a dedicated `FormatConstraintViolation`. 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 `_s` functions. That and the console's exact error value stay in the debt entry. The `vsscanf_s` part 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 `main` 8856f180c. - `guest_formatting` gets new checks for truncation and the full-length return, an empty format, null buffer and format, size 0 and above `RSIZE_MAX` (buffer untouched), `%n` and `%hhn` (pointer untouched), null `%s`, `%ls` and positional `%S`, and an unsupported conversion still throwing. On `main` these checks crash in `vsnprintf_s` with a null buffer. - Pass: `guest_formatting`, `guest_wide_formatting`, `guest_strings`. - `tools/check_conventions.py --base origin/main` passes. - Windows: not built or tested. The Windows functions already used `FormatWindows` and only gain the same wrapper. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-15 11:13:58 +00:00 (Migrated from github.com)

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.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 3987 docs/dev/debt/libc.md L36-37
PR 7081 docs/dev/debt/libc.md L37-39

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/prx/libc/include/WindowsFormatting.hpp
core/libs/tests/Formatting.cpp
docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6044 core/libs/tests/Formatting.cpp
PR 6226 docs/dev/debt/libc.md
PR 6447 core/libs/prx/libc/src/Formatting.cpp
PR 6541 docs/dev/debt/libc.md
PR 6565 docs/dev/debt/libc.md
PR 6879 core/libs/prx/libc/include/WindowsFormatting.hpp
PR 7039 docs/dev/debt/libc.md
PR 7201 core/libs/tests/Formatting.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6190725563 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6315#issuecomment-6190725563). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T11:13:58Z. --- <!-- pr-overlap --> 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**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 3987](https://www.github.com/boykopovar/AnyPS5/pull/3987) | `docs/dev/debt/libc.md` [L36-37](https://github.com/boykopovar/AnyPS5/blob/3f16b821321a466f1ea6bce1d8c5484c3fe3fb0d/docs/dev/debt/libc.md#L36-L37) | | | | [PR 7081](https://www.github.com/boykopovar/AnyPS5/pull/7081) | `docs/dev/debt/libc.md` [L37-39](https://github.com/boykopovar/AnyPS5/blob/3f16b821321a466f1ea6bce1d8c5484c3fe3fb0d/docs/dev/debt/libc.md#L37-L39) | | | 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](https://www.github.com/boykopovar/AnyPS5/pull/5021) | `core/libs/prx/libc/include/WindowsFormatting.hpp`<br>`core/libs/tests/Formatting.cpp`<br>`docs/dev/debt/libc.md` | | [PR 5303](https://www.github.com/boykopovar/AnyPS5/pull/5303) | `docs/dev/debt/libc.md` | | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/Formatting.cpp` | | [PR 6226](https://www.github.com/boykopovar/AnyPS5/pull/6226) | `docs/dev/debt/libc.md` | | [PR 6447](https://www.github.com/boykopovar/AnyPS5/pull/6447) | `core/libs/prx/libc/src/Formatting.cpp` | | [PR 6541](https://www.github.com/boykopovar/AnyPS5/pull/6541) | `docs/dev/debt/libc.md` | | [PR 6565](https://www.github.com/boykopovar/AnyPS5/pull/6565) | `docs/dev/debt/libc.md` | | [PR 6879](https://www.github.com/boykopovar/AnyPS5/pull/6879) | `core/libs/prx/libc/include/WindowsFormatting.hpp` | | [PR 7039](https://www.github.com/boykopovar/AnyPS5/pull/7039) | `docs/dev/debt/libc.md` | | [PR 7201](https://www.github.com/boykopovar/AnyPS5/pull/7201) | `core/libs/tests/Formatting.cpp` |
hypermoist commented 2026-10-16 03:15:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-16T03:15:32Z.


Rebased onto current main (5d9dcc42d). The only conflict was in docs/dev/debt/libc.md, where the entries had been reordered: the old vsnprintf_s and vsscanf_s entry is replaced by the snprintf_s and vsnprintf_s and vsscanf_s entries as before. guest_formatting and guest_wide_formatting pass and check_conventions.py is clean. #6483 (opened after this PR) changes a line in every printf-family function, so it touches vsnprintf_s and snprintf_s here too; whichever lands second will need a small rebase.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6207253571 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6315#issuecomment-6207253571). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-16T03:15:32Z. --- Rebased onto current `main` (5d9dcc42d). The only conflict was in `docs/dev/debt/libc.md`, where the entries had been reordered: the old `vsnprintf_s and vsscanf_s` entry is replaced by the `snprintf_s and vsnprintf_s` and `vsscanf_s` entries as before. `guest_formatting` and `guest_wide_formatting` pass and `check_conventions.py` is clean. #6483 (opened after this PR) changes a line in every printf-family function, so it touches `vsnprintf_s` and `snprintf_s` here too; whichever lands second will need a small rebase. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
jotitadev commented 2026-10-16 10:42:07 +00:00 (Migrated from github.com)

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. Head 40df475a8 merged onto main 72c5eed1f.

  • guest_formatting, guest_wide_formatting and guest_strings pass. With only the test applied (main's WindowsFormatting.hpp and Formatting.cpp), guest_formatting segfaults, so the new checks catch the old behaviour.
  • The rest of the suite, excluding GPU tests (-E "^agc|gpu|vulkan|video_out"), matches main: 648/650, and the two file_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:

format main (glibc) this PR
%f %+f % f with NaN, NaN, -NaN nan +nan -nan nan nan nan
%p with null (nil) 0x0
%Ld 5 throws Invalid integer length
%qd 5 throws Unsupported format conversion '%q'
%b 101 throws Unsupported format conversion '%b'
%5% % throws 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:

  • The "narrow printf family" entry in docs/dev/debt/libc.md still says "Linux retains host formatting for formats without wide arguments or %m, so a narrow %p and a NaN there still follow the host". That is no longer true for snprintf_s and vsnprintf_s, so the entry could name them as exceptions.
  • On Linux, snprintf and snprintf_s now disagree for these formats: snprintf still goes to glibc and prints (nil) and -nan.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5546637502 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6315#pullrequestreview-5546637502). Originally posted by [jotitadev](https://github.com/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. Head `40df475a8` merged onto `main` `72c5eed1f`. - `guest_formatting`, `guest_wide_formatting` and `guest_strings` pass. With only the test applied (main's `WindowsFormatting.hpp` and `Formatting.cpp`), `guest_formatting` segfaults, so the new checks catch the old behaviour. - The rest of the suite, excluding GPU tests (`-E "^agc|gpu|vulkan|video_out"`), matches main: 648/650, and the two `file_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: | format | main (glibc) | this PR | |---|---|---| | `%f %+f % f` with NaN, NaN, -NaN | `nan +nan -nan` | `nan nan nan` | | `%p` with null | `(nil)` | `0x0` | | `%Ld` | `5` | throws `Invalid integer length` | | `%qd` | `5` | throws `Unsupported format conversion '%q'` | | `%b` | `101` | throws `Unsupported format conversion '%b'` | | `%5%` | `%` | throws `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: - The "narrow printf family" entry in `docs/dev/debt/libc.md` still says "Linux retains host formatting for formats without wide arguments or `%m`, so a narrow `%p` and a NaN there still follow the host". That is no longer true for `snprintf_s` and `vsnprintf_s`, so the entry could name them as exceptions. - On Linux, `snprintf` and `snprintf_s` now disagree for these formats: `snprintf` still goes to glibc and prints `(nil)` and `-nan`. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
harmsluis commented 2026-10-17 17:45:19 +00:00 (Migrated from github.com)

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:

  • main 66702f1cf (baseline): guest_formatting, guest_wide_formatting and guest_strings pass 3/3.
  • Control, only this PR's tests/Formatting.cpp on main: guest_formatting aborts (0xc0000409) with an uncaught std::invalid_argument: Null formatting argument from FormatWindows, so the new checks reach the Windows formatter and catch the gap there.
  • This PR (40df475a8) merged onto 66702f1cf without conflicts: builds with no new warnings; guest_formatting, guest_wide_formatting and guest_strings pass 5/5 (ctest --repeat until-fail:5), and guest_standard_streams, file_streams and guest_crt pass 3/3.

Not covered: the full build, and any title run.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6237927247 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6315#issuecomment-6237927247). Originally posted by [harmsluis](https://github.com/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: - `main` `66702f1cf` (baseline): `guest_formatting`, `guest_wide_formatting` and `guest_strings` pass 3/3. - Control, only this PR's `tests/Formatting.cpp` on `main`: `guest_formatting` aborts (`0xc0000409`) with an uncaught `std::invalid_argument: Null formatting argument` from `FormatWindows`, so the new checks reach the Windows formatter and catch the gap there. - This PR (`40df475a8`) merged onto `66702f1cf` without conflicts: builds with no new warnings; `guest_formatting`, `guest_wide_formatting` and `guest_strings` pass 5/5 (`ctest --repeat until-fail:5`), and `guest_standard_streams`, `file_streams` and `guest_crt` pass 3/3. Not covered: the full build, and any title run.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!6315
No description provided.