fix(libc): check sprintf_s and vsprintf_s as titles' libc.prx does #12622

Merged
JJenkx merged 1 commit from upstream-pr/12622 into main 2026-11-16 03:58:07 +00:00
JJenkx commented 2026-10-28 23:13:14 +00:00 (Migrated from github.com)

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


What

sprintf_s and vsprintf_s currently truncate output that does not fit and omit constraint-handler calls. This validates the destination and format, then applies the previously measured rules from the libc.prx shipped by PPSA07923 and PPSA05916:

  • Output that fits returns its length.
  • A null destination, zero size or size above SonyRsizeMax returns 0 without writing and reports <name>: bad argument.
  • A null format clears the first byte, returns 0 and reports <name>: bad format argument.
  • Overflow preserves the measured size - 1 prefix, clears its first byte, returns 0 and reports <name>: buffer overflow with ERANGE.
  • %n and a null %s report the secure-conversion error and return 0. If preceding text already filled the buffer, overflow is reported afterward as well.

The Windows wrappers move next to snprintf_s so both platforms use the same FormatTerminated. The existing secure conversion parser supplies the conversions; the branch is one topic commit on main. Main's scanner, snprintf_s implementation and its existing tests are preserved. The debt update uses a fragment.

guest_sprintf_s_constraints selects the terminated-output cases independently of the rest of the formatting suite. This makes the new behavior directly testable with unchanged main production code.

Tested

Linux x86-64, GCC 16.2.1, CMake/Ninja Release. Candidate c5b8c8f8fe40721ec724fa1bd49eb277e3077340, base e8b9749c0bea648e02e57f6530378dce5d1113ac; no merge commits.

Source Result
Plain main, existing fixtures 7/7 CPU suites pass
Main production plus the candidate's identical formatting fixture and test registration 6/8 pass; guest_formatting and the isolated guest_sprintf_s_constraints fail as expected
This change 8/8 pass

No skipped or missing executables. The fixtures link core/libs/libs/unpatched/libc.prx. The actual libc and fixture binaries were rebuilt, their hashes remained unchanged through each test arm, and the mutable build checkout was restored after every arm. The seven existing suites cover formatting, scan counts, wide stream scanning, wide formatting, CRT, variadic stdio and strings. The two negative-control failures detect the terminated-output behavior rather than depending on #9087.

Conventions and debt-fragment checks pass; the origin/main check reports zero errors with inherited long-debt warnings. A separate latest-main merge preview is attached with exact pins; that preview tree was not executed. No new Windows, title, console or performance result is claimed. Prior native reference measurements retain their historical pins in the archived previous description.

Exact pins, full build/test logs, JUnit, binary hashes, source patch, preservation checks and historical description, SHA-256 631ec1b7022a0c74f0f0d525dc519b2f179d0b5eb9ef1ce9aae6c2caf416f220.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a (CPU libc tests)
  • AI-assisted: yes (Codex)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5059182754 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12622). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-28T23:13:14Z. --- ### What `sprintf_s` and `vsprintf_s` currently truncate output that does not fit and omit constraint-handler calls. This validates the destination and format, then applies the previously measured rules from the `libc.prx` shipped by PPSA07923 and PPSA05916: - Output that fits returns its length. - A null destination, zero size or size above `SonyRsizeMax` returns 0 without writing and reports `<name>: bad argument`. - A null format clears the first byte, returns 0 and reports `<name>: bad format argument`. - Overflow preserves the measured `size - 1` prefix, clears its first byte, returns 0 and reports `<name>: buffer overflow` with `ERANGE`. - `%n` and a null `%s` report the secure-conversion error and return 0. If preceding text already filled the buffer, overflow is reported afterward as well. The Windows wrappers move next to `snprintf_s` so both platforms use the same `FormatTerminated`. The existing secure conversion parser supplies the conversions; the branch is one topic commit on main. Main's scanner, `snprintf_s` implementation and its existing tests are preserved. The debt update uses a fragment. `guest_sprintf_s_constraints` selects the terminated-output cases independently of the rest of the formatting suite. This makes the new behavior directly testable with unchanged main production code. ### Tested Linux x86-64, GCC 16.2.1, CMake/Ninja Release. Candidate `c5b8c8f8fe40721ec724fa1bd49eb277e3077340`, base `e8b9749c0bea648e02e57f6530378dce5d1113ac`; no merge commits. | Source | Result | |---|---| | Plain main, existing fixtures | 7/7 CPU suites pass | | Main production plus the candidate's identical formatting fixture and test registration | 6/8 pass; `guest_formatting` and the isolated `guest_sprintf_s_constraints` fail as expected | | This change | 8/8 pass | No skipped or missing executables. The fixtures link `core/libs/libs/unpatched/libc.prx`. The actual libc and fixture binaries were rebuilt, their hashes remained unchanged through each test arm, and the mutable build checkout was restored after every arm. The seven existing suites cover formatting, scan counts, wide stream scanning, wide formatting, CRT, variadic stdio and strings. The two negative-control failures detect the terminated-output behavior rather than depending on #9087. Conventions and debt-fragment checks pass; the `origin/main` check reports zero errors with inherited long-debt warnings. A separate latest-main merge preview is attached with exact pins; that preview tree was not executed. No new Windows, title, console or performance result is claimed. Prior native reference measurements retain their historical pins in the archived previous description. [Exact pins, full build/test logs, JUnit, binary hashes, source patch, preservation checks and historical description](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr12622-c5b8-standalone-libc-linux-20261115.tar.gz), SHA-256 `631ec1b7022a0c74f0f0d525dc519b2f179d0b5eb9ef1ce9aae6c2caf416f220`. <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a (CPU libc tests) - [x] AI-assisted: <!-- yes / no --> yes (Codex)
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!12622
No description provided.