fix(libc): check swprintf_s and snwprintf_s as titles' libc.prx does #12627

Merged
JJenkx merged 4 commits from upstream-pr/12627 into main 2026-11-14 03:47:39 +00:00
JJenkx commented 2026-10-28 23:30:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-28T23:30:52Z.


What

swprintf_s, vswprintf_s, snwprintf_s and vsnwprintf_s now check their arguments and output the way the libc.prx that titles ship does. On main they called no constraint handler, returned -1 for everything, and accepted sizes up to 2^62 elements. Measured natively on PPSA07923 and PPSA05916, which agree:

case snwprintf_s, vsnwprintf_s swprintf_s, vswprintf_s
output fits length length
output too long full length, truncated, no handler 0; stores size - 1 units and clears the first; "<name>: buffer overflow" with ERANGE
null buffer, size 0, size above SIZE_MAX >> 3 elements -1, nothing written, "<name>: bad argument" 0, same
null format -1, buffer[0] = 0, "<name>: bad format argument" 0, same
%n, null %s, null %ls -23, buffer[0] cleared after the text before it; "wprintf_s: %n disallowed", "wprintf_s: bad %s argument" or "wprintf_s: bad %ls argument" 0, same; then also "<name>: buffer overflow" when that text already filled the buffer

Every report uses EINVAL (except the overflow) and a null pointer. <name> is the function's own name; the v forms use theirs too. The element limit is 2^61 - 1, the same number as the narrow byte limit, not divided by sizeof(char16_t).

Implementation: one FormatWideSecure helper replaces the four bodies. FormatWide now writes into a caller's string, so the text produced before a violation is kept, as the titles keep it in the buffer. In secure mode it throws the titles' messages.

%S: the titles print %S literally, as an unknown conversion, in all four functions. That's #12397's change, not this PR's, and the %S rows are left out of the comparison below. Merged locally with #12397, #11907 and #11893, the four functions match the titles on every row including %S, and guest_wide_formatting passes. #12397 conflicts with this PR only on the swprintf_s and vswprintf_s debt entry; the resolution keeps this PR's measured rule plus #12397's %S wording.

Not stacked on #9087: this PR doesn't touch Formatting.cpp, so it's based on main. It conflicts with #9087 only on the snwprintf_s debt entry, which both edit. The resolution is this PR's entry: with the measured limit the narrow and wide sides agree again, so #9087's sentence about them disagreeing no longer applies. orezraey asked for the wide pair as a separate PR.

Fulfilled dependency: #9935 reached main through merged #10270. The default handler writes its message and returns; integrating #9935’s exact closed head into pinned main leaves main’s tree unchanged.

Tested

Current-main integration — 2026-11-09 UTC. Head 3abfc9a3015e5a4a5e786707a23a45f4c3df4357 preserves public head 6d6c12de5195c06f94a58e6227bec1b630702aa7 and integrates pinned main 7259cce11195f360698772da8c0b3af756bf7c8f. Conflict resolution retains both the topic and main's newer source/test/debt changes; maintainer commits and attribution remain intact.

Linux x86-64, GCC 16.2.1, Ninja Release: actual libc/libkernel and focused fixture owners rebuilt. 15/15 CPU executions pass, zero skips (5 groups × three repetitions): guest_wide_formatting, guest_wide_stream_scan, guest_formatting, guest_stdio_variadic, guest_crt. public_headers, conventions and whitespace checks pass. The matching plain-main control passed 102 selected CPU executions and its header audit. CPU fixtures execute unpatched production libraries; this is not a full SDK or patched-title runtime qualification.

No new Windows/macOS, console, GPU, title or performance result is claimed. Source checks preserve the existing production topic; historical reports below retain their original pins.

Build/test logs, exact heads, source/ancestry checks and provider pins, SHA-256 52727be6594b9c15bf9a5535fef374b985e9526aabe5f3ef5cf5857dfeeb1a0d. The public archive and all 178 manifest files were download/hash verified.

Current-main integration — 2026-11-04. Head 6d6c12de5195c06f94a58e6227bec1b630702aa7 includes main b11c7bf84f5d2f636ff60166cc3371bd071e24d5 and preserves previous public commits and attribution/licence notices. Keeps the measured wide secure-format constraints and the concise %S limitation. Main’s default constraint handler now writes and returns through the fulfilled #10270/#9935 change.

Linux x86-64, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON: rebuilt actual libc/Ampr and selected executables. guest_wide_formatting, guest_formatting, guest_stdio_variadic, guest_wide_conversion, guest_scan_count, guest_wide_stream_scan each pass 3/3 (18 runtime passes, zero skips); public_headers, conventions and whitespace checks pass. Pinned main passes all 17 selected baseline groups 3/3 (51 passes) plus headers. Before/after dependency checks agree: 34 recursive trees and 31,551 tracked blobs, zero errors; the update=none QuickJS test262 corpus is explicitly excluded from these library inputs.

Source/parent/gitlink proofs, library/test hashes, raw logs/JUnit and head-leased push receipts: qualification archive, SHA-256 7c347f620951ad6f39068740750949212c6412815da094a46a96eca1d15c8bbf (download verified). Older platform, stress, negative-control and reference reports retain their original pins; no new Windows/macOS, full-suite, SDK-module, console, title/GPU or performance claim. AI-assisted: yes (Codex).

Earlier dated qualifications below retain their original source/platform pins.

Latest Linux qualification (2026-11-02): exact head e1becbe8feb67df3e585fb8f662e21eb773d3633 on pinned main a4eca2a056bf3bbbec98ab42e9d28163e4ea9cb8. Keeps main’s bool AppendUtf16/WideEncodingError handling and the measured secure null-string constraint report. The previously restored f8fe5ccc wide%S debt remains; #12397 is not integrated. The obsolete pre-9953 byte-copy encoding claim is narrowed to the still-unverified secure encoding behavior.

4 focused groups repeated three times: 12 passes, zero skips. Public headers, conventions, whitespace, raw libc/test builds and the actual production NID-patching command passed. MinGW syntax passed for Formatting.cpp, FormattingWide.cpp. No new Windows/macOS runtime, bundled-SDK reference, native title, GPU or performance claim; earlier evidence retains its historical pins.

Earlier pinned qualifications follow.

Windows 11, WinLibs GCC 15.2.0 UCRT r7, Release, main 319621ba2e.

  • Native comparison: a scratch loader calls the titles' functions by NID next to this build's, with the same recording handler on both sides. The v forms are called through a hand-built System V va_list; the same loader reproduces #9087's vsnprintf_s results exactly, which checks that construction. Each case compares the return value, the handler's calls, error, pointer and message, and the buffer contents.
    • swprintf_s and snwprintf_s, 18 cases each: fits, fits exactly, too long, size 1, size 0, null buffer, null format, 2^60, 2^60 + 1, 2^61 - 1, 2^61, SIZE_MAX, %n, null %s, null %ls, an empty format, and %n past the end. main differs in 22 of 36; this PR: 0.
    • vswprintf_s and vsnwprintf_s, 10 cases each: 0 of 20 differ.
    • The %S rows are left out, as described above.
  • Regression: guest_wide_formatting installs a recording handler.
    • CheckSecureWideConstraints checks every row of the table for all four functions.
    • CheckSecureWideBoundary now uses the measured 2^61 - 1 limit instead of (SIZE_MAX >> 1) / 2.
    • With main's FormattingWide.cpp the test fails.
  • Test set: guest_wide_formatting, guest_safe_errors, guest_formatting, guest_wide_strings, guest_wide_stream_scan and guest_wide_conversion pass 3/3. Conventions pass.

Merge order: the existing agreement with orezraey is retained for the remaining open #9087: this PR lands first, then #9087 integrates main while keeping these wide-format debt entries. #10270 has already merged and is fulfilled history.

Current qualification — 2026-10-30

Integrated upstream main 08a10d225306ad102b57408e2ef7566bcfb829b2 at c713fbf818cfb2ca97bf771ab290140b0ebaadb1, retaining the actual previous head and main as parents. Manual conflict resolution changes only debt documentation; production and test files equal the automatic merge output. Both parents' added test lines and main gitlinks are preserved.

Linux CPU: 9 passes (guest_wide_formatting, guest_formatting, guest_crt); public_headers 1 passes; no skips. MinGW source syntax and conventions pass.

No new Windows/macOS runtime, native SDK, game, GPU or performance claim. The Windows user pause remains in force.

Evidence: immutable source and qualification receipts, SHA-256 61f5500e7526ea4e107159927de78c37db7e0ac1cb7ebd8625b22ab0ad5d9bd7 (download verified).

Independent Windows validation, reported by bunbeans2-arch for topic c713fbf818cfb2ca97bf771ab290140b0ebaadb1 against main f7f03daf52: Windows 11 Pro 22631, i9-9900K, MSYS2 UCRT64 GCC 16.1, Release. guest_wide_formatting passes 5/5; retaining the new test with main FormattingWide.cpp fails 5/5 at the accepted-boundary clearing assertion; plain main passes 3/3. This is the contributor's run, not a new local Windows execution.

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 (#9935 fulfilled through merged #10270; agreed #9087 order above retained)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

Current-main refresh (2026-11-12). Public head 600a910b82e1da60ca48b2bc185e0395d5a868c1 integrates main 59f3a64cf61fbe4a63699a4d61ae734b4375df03, preserving the prior public commits and maintainer edits. The only conflict was two adjacent test calls: both this PR's CheckSecureWideConstraints() and main's CheckMeasuredWideConversions() are retained. All 266 changed production/test lines are identical to the prior topic relative to its base; main's newer conversions stay intact.

Actual libc and these six test executables rebuilt separately on main and the refreshed branch: guest_wide_formatting, guest_formatting, guest_stdio_variadic, guest_wide_conversion, guest_scan_count, guest_wide_stream_scan. Each group passed three times per arm: 18/18 main, 18/18 change, zero skips. Source identity/cleanliness checks, conventions, whitespace and the final latest-main merge preview pass. The ordinary main fixtures ran on main; this is maintenance qualification, not a fresh bundled-library semantic measurement or a new negative control.

Exact pins, commands, both build logs, full test output/JUnit and preservation proof — SHA-256 7ce20289120304e979bab15ba92c3d3c80196b47f89a4d5b07bd8a43b26ffa16; downloaded and all 20 archive members verified. Prior Windows/reference/other-platform evidence retains its original pins. No fresh Windows, GPU/native title, full SDK or performance claim.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5059366659 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12627). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-28T23:30:52Z. --- ### What `swprintf_s`, `vswprintf_s`, `snwprintf_s` and `vsnwprintf_s` now check their arguments and output the way the `libc.prx` that titles ship does. On `main` they called no constraint handler, returned -1 for everything, and accepted sizes up to `2^62` elements. Measured natively on PPSA07923 and PPSA05916, which agree: | case | `snwprintf_s`, `vsnwprintf_s` | `swprintf_s`, `vswprintf_s` | |---|---|---| | output fits | length | length | | output too long | full length, truncated, no handler | 0; stores `size - 1` units and clears the first; `"<name>: buffer overflow"` with `ERANGE` | | null buffer, size 0, size above `SIZE_MAX >> 3` elements | -1, nothing written, `"<name>: bad argument"` | 0, same | | null format | -1, `buffer[0] = 0`, `"<name>: bad format argument"` | 0, same | | `%n`, null `%s`, null `%ls` | -23, `buffer[0]` cleared after the text before it; `"wprintf_s: %n disallowed"`, `"wprintf_s: bad %s argument"` or `"wprintf_s: bad %ls argument"` | 0, same; then also `"<name>: buffer overflow"` when that text already filled the buffer | Every report uses `EINVAL` (except the overflow) and a null pointer. `<name>` is the function's own name; the `v` forms use theirs too. The element limit is `2^61 - 1`, the same number as the narrow byte limit, not divided by `sizeof(char16_t)`. **Implementation:** one `FormatWideSecure` helper replaces the four bodies. `FormatWide` now writes into a caller's string, so the text produced before a violation is kept, as the titles keep it in the buffer. In secure mode it throws the titles' messages. **`%S`:** the titles print `%S` literally, as an unknown conversion, in all four functions. That's #12397's change, not this PR's, and the `%S` rows are left out of the comparison below. Merged locally with #12397, #11907 and #11893, the four functions match the titles on every row including `%S`, and `guest_wide_formatting` passes. #12397 conflicts with this PR only on the `swprintf_s and vswprintf_s` debt entry; the resolution keeps this PR's measured rule plus #12397's `%S` wording. **Not stacked on #9087:** this PR doesn't touch `Formatting.cpp`, so it's based on `main`. It conflicts with #9087 only on the `snwprintf_s` debt entry, which both edit. The resolution is this PR's entry: with the measured limit the narrow and wide sides agree again, so #9087's sentence about them disagreeing no longer applies. orezraey [asked](https://github.com/boykopovar/AnyPS5/pull/9087#issuecomment-6482186921) for the wide pair as a separate PR. **Fulfilled dependency:** #9935 reached main through merged #10270. The default handler writes its message and returns; integrating #9935’s exact closed head into pinned main leaves main’s tree unchanged. ### Tested <!-- codex-maintenance-20261109-cpu-12627 --> **Current-main integration — 2026-11-09 UTC.** Head `3abfc9a3015e5a4a5e786707a23a45f4c3df4357` preserves public head `6d6c12de5195c06f94a58e6227bec1b630702aa7` and integrates pinned main `7259cce11195f360698772da8c0b3af756bf7c8f`. Conflict resolution retains both the topic and main's newer source/test/debt changes; maintainer commits and attribution remain intact. Linux x86-64, GCC 16.2.1, Ninja Release: actual libc/libkernel and focused fixture owners rebuilt. **15/15 CPU executions pass, zero skips** (5 groups × three repetitions): `guest_wide_formatting`, `guest_wide_stream_scan`, `guest_formatting`, `guest_stdio_variadic`, `guest_crt`. `public_headers`, conventions and whitespace checks pass. The matching plain-main control passed 102 selected CPU executions and its header audit. CPU fixtures execute unpatched production libraries; this is not a full SDK or patched-title runtime qualification. No new Windows/macOS, console, GPU, title or performance result is claimed. Source checks preserve the existing production topic; historical reports below retain their original pins. [Build/test logs, exact heads, source/ancestry checks and provider pins](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/cpu8-main7259-linux-20261109.tar.gz), SHA-256 `52727be6594b9c15bf9a5535fef374b985e9526aabe5f3ef5cf5857dfeeb1a0d`. The public archive and all 178 manifest files were download/hash verified. **Current-main integration — 2026-11-04.** Head `6d6c12de5195c06f94a58e6227bec1b630702aa7` includes main `b11c7bf84f5d2f636ff60166cc3371bd071e24d5` and preserves previous public commits and attribution/licence notices. Keeps the measured wide secure-format constraints and the concise `%S` limitation. Main’s default constraint handler now writes and returns through the fulfilled #10270/#9935 change. Linux x86-64, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON: rebuilt actual libc/Ampr and selected executables. `guest_wide_formatting`, `guest_formatting`, `guest_stdio_variadic`, `guest_wide_conversion`, `guest_scan_count`, `guest_wide_stream_scan` each pass 3/3 (**18 runtime passes, zero skips**); `public_headers`, conventions and whitespace checks pass. Pinned main passes all 17 selected baseline groups 3/3 (51 passes) plus headers. Before/after dependency checks agree: 34 recursive trees and 31,551 tracked blobs, zero errors; the update=none QuickJS test262 corpus is explicitly excluded from these library inputs. Source/parent/gitlink proofs, library/test hashes, raw logs/JUnit and head-leased push receipts: [qualification archive](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math-libc10-mainb11c-linux-20261104.tar.gz), SHA-256 `7c347f620951ad6f39068740750949212c6412815da094a46a96eca1d15c8bbf` (download verified). Older platform, stress, negative-control and reference reports retain their original pins; no new Windows/macOS, full-suite, SDK-module, console, title/GPU or performance claim. AI-assisted: yes (Codex). Earlier dated qualifications below retain their original source/platform pins. Latest Linux qualification (2026-11-02): exact head `e1becbe8feb67df3e585fb8f662e21eb773d3633` on pinned main `a4eca2a056bf3bbbec98ab42e9d28163e4ea9cb8`. Keeps main’s bool AppendUtf16/WideEncodingError handling and the measured secure null-string constraint report. The previously restored f8fe5ccc wide%S debt remains; #12397 is not integrated. The obsolete pre-9953 byte-copy encoding claim is narrowed to the still-unverified secure encoding behavior. 4 focused groups repeated three times: **12 passes, zero skips**. Public headers, conventions, whitespace, raw libc/test builds and the actual production NID-patching command passed. MinGW syntax passed for Formatting.cpp, FormattingWide.cpp. No new Windows/macOS runtime, bundled-SDK reference, native title, GPU or performance claim; earlier evidence retains its historical pins. Earlier pinned qualifications follow. Windows 11, WinLibs GCC 15.2.0 UCRT r7, Release, `main` `319621ba2e`. - **Native comparison:** a scratch loader calls the titles' functions by NID next to this build's, with the same recording handler on both sides. The `v` forms are called through a hand-built System V `va_list`; the same loader reproduces #9087's `vsnprintf_s` results exactly, which checks that construction. Each case compares the return value, the handler's calls, error, pointer and message, and the buffer contents. - `swprintf_s` and `snwprintf_s`, 18 cases each: fits, fits exactly, too long, size 1, size 0, null buffer, null format, `2^60`, `2^60 + 1`, `2^61 - 1`, `2^61`, `SIZE_MAX`, `%n`, null `%s`, null `%ls`, an empty format, and `%n` past the end. `main` differs in 22 of 36; this PR: **0**. - `vswprintf_s` and `vsnwprintf_s`, 10 cases each: **0 of 20 differ**. - The `%S` rows are left out, as described above. - **Regression:** `guest_wide_formatting` installs a recording handler. - `CheckSecureWideConstraints` checks every row of the table for all four functions. - `CheckSecureWideBoundary` now uses the measured `2^61 - 1` limit instead of `(SIZE_MAX >> 1) / 2`. - With `main`'s `FormattingWide.cpp` the test fails. - **Test set:** `guest_wide_formatting`, `guest_safe_errors`, `guest_formatting`, `guest_wide_strings`, `guest_wide_stream_scan` and `guest_wide_conversion` pass 3/3. Conventions pass. **Merge order:** the existing agreement with orezraey is retained for the remaining open #9087: this PR lands first, then #9087 integrates main while keeping these wide-format debt entries. #10270 has already merged and is fulfilled history. <!-- codex-maintenance-20261030-repeat-12627 --> **Current qualification — 2026-10-30** Integrated upstream main `08a10d225306ad102b57408e2ef7566bcfb829b2` at `c713fbf818cfb2ca97bf771ab290140b0ebaadb1`, retaining the actual previous head and main as parents. Manual conflict resolution changes only debt documentation; production and test files equal the automatic merge output. Both parents' added test lines and main gitlinks are preserved. Linux CPU: 9 passes (guest_wide_formatting, guest_formatting, guest_crt); public_headers 1 passes; no skips. MinGW source syntax and conventions pass. No new Windows/macOS runtime, native SDK, game, GPU or performance claim. The Windows user pause remains in force. Evidence: [immutable source and qualification receipts](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/cpu-seven-current-main-linux-20261030.tar.gz), SHA-256 `61f5500e7526ea4e107159927de78c37db7e0ac1cb7ebd8625b22ab0ad5d9bd7` (download verified). Independent Windows validation, reported by [bunbeans2-arch](https://github.com/boykopovar/AnyPS5/pull/12627#issuecomment-6517263320) for topic `c713fbf818cfb2ca97bf771ab290140b0ebaadb1` against main `f7f03daf52`: Windows 11 Pro 22631, i9-9900K, MSYS2 UCRT64 GCC 16.1, Release. `guest_wide_formatting` passes 5/5; retaining the new test with main `FormattingWide.cpp` fails 5/5 at the accepted-boundary clearing assertion; plain main passes 3/3. This is the contributor's run, not a new local Windows execution. ### 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 (#9935 fulfilled through merged #10270; agreed #9087 order above retained) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code) <!-- pr12627-current-main-refresh-20261112 --> **Current-main refresh (2026-11-12).** Public head `600a910b82e1da60ca48b2bc185e0395d5a868c1` integrates main `59f3a64cf61fbe4a63699a4d61ae734b4375df03`, preserving the prior public commits and maintainer edits. The only conflict was two adjacent test calls: both this PR's `CheckSecureWideConstraints()` and main's `CheckMeasuredWideConversions()` are retained. All 266 changed production/test lines are identical to the prior topic relative to its base; main's newer conversions stay intact. Actual libc and these six test executables rebuilt separately on main and the refreshed branch: `guest_wide_formatting`, `guest_formatting`, `guest_stdio_variadic`, `guest_wide_conversion`, `guest_scan_count`, `guest_wide_stream_scan`. Each group passed three times per arm: **18/18 main, 18/18 change, zero skips**. Source identity/cleanliness checks, conventions, whitespace and the final latest-main merge preview pass. The ordinary main fixtures ran on main; this is maintenance qualification, not a fresh bundled-library semantic measurement or a new negative control. [Exact pins, commands, both build logs, full test output/JUnit and preservation proof](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr12627-600a910-main59f3-wide-format-linux-20261112.tar.gz) — SHA-256 `7ce20289120304e979bab15ba92c3d3c80196b47f89a4d5b07bd8a43b26ffa16`; downloaded and all 20 archive members verified. Prior Windows/reference/other-platform evidence retains its original pins. No fresh Windows, GPU/native title, full SDK or performance claim.
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!12627
No description provided.