fix(libc): follow snprintf_s and sscanf_s constraints as measured on a PS5 #9087

Open
orezraey wants to merge 31 commits from upstream-pr/9087 into main
orezraey commented 2026-10-21 12:01:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-21T12:01:33Z.


What

snprintf_s, vsnprintf_s, sscanf_s and vsscanf_s followed C11 Annex K with guessed constants. Their debt entries said the constraint handler is not invoked and the console's values are unverified. In a payload process on a PS5 (firmware 13.60, libSceLibcInternal.sprx) most of those guesses differ.

snprintf_s / vsnprintf_s (both behave the same; errno never changes):

Case Payload process, firmware 13.60 main before this PR here now
RSIZE_MAX 2^61 - 1 (a boundary search: 0x1fffffffffffffff passes, 0x2000000000000000 fails) SIZE_MAX >> 1 2^61 - 1
null buffer, size 0 or above RSIZE_MAX -1, buffer untouched, handler called with "snprintf_s: bad argument" (or vsnprintf_s: …), a null pointer and 22 -1, no handler, SIZE_MAX >> 1 accepted as on the console
null format -1, buffer[0] = 0, handler "…: bad format argument" -1, buffer[0] = 0, no handler as on the console
%n / %hn (also after truncation) -23; the output before it stays written without a terminator, then buffer[0] = 0; handler "printf_s: %n disallowed"; the pointer is not written -1, terminated output as on the console
null %s argument (also %.0s) -23, same buffer handling, handler "printf_s: bad %s argument" -1, no handler as on the console
null %ls / %S argument -23, handler "printf_s: bad %ls argument" -1, no handler as on the console

sscanf_s / vsscanf_s (both behave the same):

Case Payload process, firmware 13.60 main before this PR here now
size argument 64 bits: a size of 2^32 + 2 is used as such read as unsigned int 64 bits
%s / %[ field of size or more characters the first size characters stored without a terminator; matching failure, returns the items assigned so far buffer[0] = 0 as on the console
%c with a width above size stores size characters and succeeds, consuming only those failure, buffer[0] = 0 as on the console
%ls / %l[ and %lc with a width above size (titles' own libc.prx, measured by JJenkx) %ls and %l[ follow the narrow rules; %lc stores size units and succeeds, but consumes the whole width %lc consumed only the stored characters as measured
size above RSIZE_MAX handler "scanf_s: bad %c, %s, or %[ size" not checked as on the console
null %s / %c / %[ destination handler "scanf_s: bad %c, %s, or %[ argument", checked before reading input (also on empty input) EOF, no handler as on the console
null integer, %p or floating-point destination checked only after a successful conversion: empty input gives EOF and "x" gives 0, without the handler EOF as on the console
result of any violation the handler with the console's message, a null pointer and 22; returns the assignments so far, or -23 when no conversion (suppressed ones and %n included) has completed EOF as on the console
null input or format EOF, handler "sscanf_s: bad argument" / "vsscanf_s: bad argument" EOF, no handler as on the console
literal mismatch at the end of input ("" with "x%d") 0 EOF 0

fscanf_s / vfscanf_s / scanf_s: a literal mismatch at the end of input now returns the assignments, as sscanf_s does: empty input with "x%2c%n" returns 0 and leaves errno, the %n target and the destination untouched, following JJenkx's CPU-only run of two reference libc modules (earlier heads and main return EOF). %% at the end of input still returns EOF. A %c field takes the same width rule and size clamp as in sscanf_s: %8c%n on abc with size 2 stores ab, sets the count to 2 and leaves c next; with size 4 it stores abc; on abcdef with size 4 it stores abcd and leaves e next (JJenkx's reference rows; main returned 0 before reading). The stream forms were not run on a console.

The -23 is the same with a recording handler, an empty one and ignore_handler_s. set_constraint_handler_s, abort_handler_s and ignore_handler_s are exported, and passing NULL reinstalls the default.

The formatter's terminator: to leave the partial output unterminated on a violation, FormatOutput now writes the terminator once at the end instead of after every piece. Every caller goes through FormatWindows, which does that at the end. On success every output is the same as before, which the existing formatting tests check.

Not measured, recorded in docs/dev/debt/libc.md:

  • The console's default handler writes "<message>\n" before ending the process; AnyPS5's default now writes the message and returns, as the libc.prx titles ship does (#9935's change, which reached main with #10270).
  • An unknown conversion such as %y is copied to the output by the console and still throws here.
  • A null %hs argument was not measured.
  • swscanf_s's size width (it still reads unsigned int) was not measured.

Tests: guest_formatting, guest_scan_count (also JJenkx's wide rows) and guest_strings check the measured cases, with the old expectations updated and size arguments passed as size_t. guest_scan_count fails if the -23 constant is changed.

How the console was measured

  • Setup:
    • The console runs firmware 13.60 and is jailbroken.
    • The probes are ELF payloads built with ps5-payload-dev/sdk v0.44 and clang 18.1.8 and started through elfldr, so they ran in a payload process, not in a title.
  • Provenance: libSceLibcInternal.sprx was loaded with dlopen, and every function was taken from it with dlsym.
  • Method:
    • Buffers are filled with ~ and integer destinations preset to -7.
    • A recording constraint handler logs the message, pointer and error of each call.
    • libcfmt3 and libcfmt4 reinstall the default handler and end their own process with it, to see what it does.
  • Files: the probe sources and full logs are in this gist.
Console output (excerpt, copied unchanged)
libcfmt: meta fw=0x13600007 pid=153 sdk=v0.44 compiler=Clang 18.1.8 built=Oct 21 2026 08:20:42 log=/data/anyps5-probes/libcfmt.log
libcfmt: case snprintf_s truncate size=5 "abcdef" ret=6 errno=0 buf="abcd\x00~~~~~~~~~~~" handler=0
libcfmt: case snprintf_s size=0 ret=-1 errno=0 buf="~~~~~~~~~~~~~~~~" handler=1 msg="snprintf_s: bad argument" err=22 ptr_off=-2
libcfmt: case size_boundary last_ret_1=0x1fffffffffffffff first_ret_-1=0x2000000000000000
libcfmt: case snprintf_s %n ret=-23 errno=0 buf="\x00b~~~~~~~~~~~~~~" handler=1 msg="printf_s: %n disallowed" err=22 ptr_off=-2
libcfmt: case vsscanf_s "abc1" %[a-z] cap=4 ret=1 errno=0 n1=-7 n2=-7 a="abc\x00~~~~" b="~~~~~~~~" handler=0
libcfmt: case vsscanf_s "abcd1" %[a-z] cap=4 ret=0 errno=0 n1=-7 n2=-7 a="abcd~~~~" b="~~~~~~~~" handler=0
libcfmt: case vsscanf_s "x" %3c cap=2 ret=1 errno=0 n1=-7 n2=-7 a="x~~~~~~~" b="~~~~~~~~" handler=0
libcfmt2: case vsscanf_s null %s first ret=-23 n1=-7 n2=-7 f=-7 a="~~~~~~~~" b="~~~~~~~~" handler=1 msg="scanf_s: bad %c, %s, or %[ argument" err=22
libcfmt2: case vsscanf_s "ab" %s cap=2^61 ret=-23 n1=-7 n2=-7 f=-7 a="~~~~~~~~" b="~~~~~~~~" handler=1 msg="scanf_s: bad %c, %s, or %[ size" err=22
libcfmt3: case vsscanf_s "5 ab" %d %s(null) %d ret=1 n1=5 n2=-7 f=-7 a="~~~~~~~~" b="~~~~~~~~" handler=1 msg="scanf_s: bad %c, %s, or %[ argument" err=22

Tested

  • Head 1c3b118df8: main (0263899e99, with #13119) was merged in. Formatting.cpp met in two places: the %n bookkeeping keeps both #13119's ordinary rule and this PR's secure contiguous one, and sscanf uses #13119's ScansInProcess with this PR's null secure name. As agreed with JJenkx, the shared stream %c loop now takes a %c width by its low 32 bits (zero or above INT_MAX reads one) and clamps a secure field to its size instead of failing before reading; guest_stdio_variadic gains five stream rows from JJenkx's matrix (%8c%n on abc with sizes 2 and 4, on abcdef with size 4, %2147483648c and %4294967298c), which fail when the clamp is removed. tests/Formatting.cpp keeps this PR's unterminated %s check and #13119's CheckOrdinaryCharacterScans; libc.md keeps this PR's entries, and #13119's sentence about the secure %c width and size is dropped since both paths now follow those references. At this head guest_formatting, guest_scan_count, guest_stdio_variadic, file_streams and guest_strings pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL, GCC); check_conventions reports no errors (one debt-length warning on a libc.md entry this PR did not add).
  • Head d44f653788: main (b8efa6292a, with #11907) was merged in. Only core/libs/tests/Formatting.cpp met, in CheckSecureFormat: it keeps this PR's measured snprintf_s/vsnprintf_s checks and adds main's new cases, [%y][%-4k] printed literally (with no constraint call) and %hhn refused with the buffer emptied and the destination untouched. At this head guest_formatting, guest_wide_formatting, guest_scan_count, guest_stdio_variadic and guest_strings pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL, GCC 16.2), and check_conventions reports 0 errors.
  • Head 0f9bb22b03: main (c2eafe4461, with #9863 and #10270) was merged in. Only libc.md met: it keeps main's wcsncat_s and set_constraint_handler_s entries (the default handler now writes the message and returns), #9863's short-field wording in the _WStoul/swscanf_s entry with this PR's measured sscanf_s comparison, and this PR's snwprintf_s sentence. Formatting.cpp now uses SonyRsizeMax from SonyCrt.hpp (same 2^61 - 1) instead of its own secureSizeMax, as agreed with harisibrar1 on this PR. At this head guest_formatting, guest_scan_count, guest_stdio_variadic, guest_strings and guest_safe_errors pass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL, GCC 16.2); check_conventions reports no errors (one debt-length warning on the long _WStoul entry).
  • Head d826fd16d3: main was merged in, with #12428; the unknown-conversion exit now comes before the secure block. guest_formatting, guest_wide_formatting, guest_scan_count, guest_stdio_variadic, guest_safe_errors and guest_strings pass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL, GCC); check_conventions passes. Disregard the Linux result recorded for 24f4242cbd: another checkout changed the shared tree while that build ran. This Linux run replaces it, on a tree nothing else touched.
  • Head 24f4242cbd: main (1d91f2966d, with #12468) was merged in and the secure bounds were re-applied on its source scanner: for sscanf_s and vsscanf_s, %ls, %lc and %l[ keep the first size characters without a terminator when they do not fit, and %lc consumes the rest of its width, which now counts bytes as #12468's loop does. Stream forms keep main's behaviour. libc.md keeps #12468's scanf_s wording and this PR's. At this head guest_formatting, guest_scan_count, guest_wide_formatting, guest_stdio_variadic, guest_strings, guest_wide_strings and guest_safe_errors pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions reports no errors.
  • Head e16c92d39f merges main (93dbd0f82a, with #8397); only the sscanf_s debt line met #8397's new _LDnorm entry, and both are kept. At this head the twelve tests below pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL, GCC 16.2), and check_conventions reports no errors (one debt-length warning on a libc.md entry this PR did not add).
  • Head 053ebd8a33: main (1ff6c8739b) was merged in. Only libc.md met, at the scanf_s and freopen_s entries: it keeps this PR's scanf_s wording with JJenkx's end-of-input result and main's new freopen_s entry with its constraint handler calls. At this head guest_formatting, guest_scan_count, guest_wide_formatting, guest_stdio_variadic, guest_strings and guest_safe_errors pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions passes (the one debt-length warning on the _WStoul entry, as before).
  • Head 29d8de386f: main (3b32cc7b61, with #12420) was merged in. Only libc.md met, in the _WStoul/swscanf_s entry: it keeps #12420's scanset range, length and %p wording and this PR's measured sscanf_s comparison (64-bit size, first size characters stored without a terminator). At this head guest_formatting, guest_scan_count, guest_wide_formatting, guest_stdio_variadic, guest_strings and guest_safe_errors pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions passes (the one debt-length warning on the _WStoul entry, as before).
  • Head d00ac4ec91: main (0362b1521c, with #12933) was merged in. Only libc.md met: it keeps #12933's new dprintf/vdprintf entry and this PR's measured snprintf_s/vsnprintf_s entry, with the old vsscanf_s entry still removed. At this head guest_formatting, guest_scan_count, guest_wide_formatting, guest_stdio_variadic, guest_strings and guest_safe_errors pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions passes (the one debt-length warning on the _WStoul entry, as before).
  • Head 4eaf64fee1: sscanf_s/vsscanf_s take a %c width by its low 32 bits and read one character when it is zero or above INT_MAX, following JJenkx's reference rows (%2147483648c on abc stores one byte with size 2 or 4; %4294967298c reads two). guest_scan_count gains those three rows, which fail on 16a242bbaa. The stream scanner keeps main's path until #13057 and #13119 land. At this head guest_formatting, guest_scan_count, guest_wide_formatting, guest_stdio_variadic, guest_strings and guest_safe_errors pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions passes (the one debt-length warning on the _WStoul entry, as before).
  • Head 16a242bbaa: the stream scanner (fscanf_s, vfscanf_s, scanf_s) returns the assignments for a literal mismatch at the end of input, following JJenkx's reference rows. guest_stdio_variadic gains the empty-input "x%2c%n" case (0, errno 97, %n -77 and the 0xa5 destination untouched) and %% at the end of file (EOF); the first check fails on the previous head. The twelve tests listed below pass three runs in a row on Windows (WinLibs GCC) and on Linux (WSL Ubuntu, GCC); check_conventions passes (the existing _WStoul debt-length warning only).
  • Head 3b0fa2fc4d: main (157b02c4f1, with #11852) was merged in. Only Formatting.cpp met, in sscanf: it keeps #11852's cut at a q length directive (applied to sscanf and vsscanf only, as on main) and this PR's ScanGuest signature with a null secure name. At this head guest_formatting, guest_scan_count, guest_stdio_variadic, guest_strings and guest_safe_errors pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions passes (the one debt-length warning on the _WStoul entry, as before).
  • Head ec862a495a: %lc with a width above size consumes the whole width, following JJenkx's run of titles' libc.prx (PPSA07923, PPSA05916). guest_scan_count gains CheckWideFields with the eight rows of that table, and its two %lc rows fail on acb1ec494f. At this head guest_formatting, guest_scan_count, guest_stdio_variadic, guest_strings and guest_safe_errors pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions passes (the one debt-length warning on the _WStoul entry, as before).
  • Head acb1ec494f: main (f80ce6cec1, with #11121) was merged in. Only libc.md met: it keeps this PR's snprintf_s/vsnprintf_s and set_constraint_handler_s wording and its removal of the old vsscanf_s entry, and main's localeconv entry with its removal of the setlocale entry. At this head guest_formatting, guest_scan_count, guest_stdio_variadic, guest_strings and guest_safe_errors pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions passes (the one debt-length warning on the _WStoul entry, as before).
  • Head 0055557985: main (5bdda3661e, with #10176 and #9973) was merged in. Only libc.md met: it keeps this PR's measured sscanf_s/vsscanf_s and _WStoul/swscanf_s entries, main's firmware 13.60 export note on memmem and memccpy and #9973's new floating scan errno entry. At this head the twelve tests below pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); check_conventions passes (the one debt-length warning on the _WStoul entry, as before).
  • Head 2f05aac2d2: main (5b85225ef3, with #9527's ScanSource) was merged in 5652af2d1a. This PR's measured sscanf_s/vsscanf_s rules were ported onto ScanSource for StringSource only; StreamSource keeps main's fscanf_s/scanf_s path. 2f05aac2d2 makes #9527's GuestStdioVariadic checks expect the measured snprintf_s (-23 with a handler call) and undersized sscanf_s %s results instead of the old ones. At this head the twelve tests below pass three runs in a row on Windows and on Linux, and the 49 libc tests matching scan|format|print|string|stdio|safe|constraint|bounds|crt|locale|wide pass on Windows. Before that, head after merging main again (9dbc017c71, with #9479): efeb582327; only core/libs/tests/Formatting.cpp met, keeping this PR's constraint-handler helpers and measured sscanf_s too-small %s check together with #9479's CheckIntegerScanWidths and its RunChecks call. At this head the twelve tests below pass three runs in a row on Windows and on Linux (the Linux public_headers audit times out at 30 s on the WSL /mnt/c checkout, unrelated to this change). Before that, main (ac65cf490e, with #9613) was merged in b824e3b277; only the strndup and snprintf_s debt lines met, keeping #9613's strndup disassembly reading and this PR's measured snprintf_s entry (the old vsscanf_s line stays removed, as its measured sscanf_s entry replaces it). At this head the twelve tests below pass three runs in a row on Windows and on Linux. Before that, main (66e6772ca2, with #9360) was merged in 6b9e2f1574; only the snwprintf_s and swprintf_s debt lines met, keeping this PR's snwprintf_s wording and main's swprintf_s limit. Before that, main (f26434afa3, with #10706) was merged in 7b400cbcfe. The tests take their declarations from libc/include/Api.hpp. guest_formatting, guest_scan_count, guest_wide_formatting, guest_wide_stream_scan, guest_stdio_variadic, guest_locale, guest_strings, guest_wide_strings, guest_wide_conversion, guest_safe_errors, guest_crt and guest_bounds_checking pass three runs in a row on Windows (WinLibs GCC) and on Linux (WSL Ubuntu, GCC), which now also builds the #ifndef _WIN32 wide scan path. public_headers audit and check_conventions pass (one debt-length warning on the _WStoul entry, already 3083 characters on main).
    Windows 11, WinLibs GCC 15.2.0, Release: the 30 libc tests whose names cover formatting, scanning, strings, wide characters, streams, locales and bounds checking pass. Linux not built: the #ifndef _WIN32 wide scan path in ScanGuest was changed with the narrow rules and only passed a g++ syntax check, so this PR relies on the Linux CI job to build and run it.

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: #7081, #7386, #9340 edit the same lines (merge order only); #9935's default constraint handler, which writes the message and returns so these new handler calls cannot end a title, reached main with #10270; #7851 (hypermoist) adds libc.md entries next to the ones this PR edits (merge order only); #12574 (JJenkx) edits libc.md lines this PR changes (merge order only); #12627 (JJenkx) rewrites the snwprintf_s and swprintf_s entries this PR edits in libc.md (merge order only); #12598 (JJenkx) rewrites the qsort_s entry next to the libc.md entries this PR edits (merge order only); #9688, #9863, #10729, #11621, #11907, #12397, #12468, #12530, #12532 and #13119 have merged
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4951197877 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9087). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-21T12:01:33Z. --- ### What `snprintf_s`, `vsnprintf_s`, `sscanf_s` and `vsscanf_s` followed C11 Annex K with guessed constants. Their debt entries said the constraint handler is not invoked and the console's values are unverified. In a payload process on a PS5 (firmware 13.60, `libSceLibcInternal.sprx`) most of those guesses differ. **snprintf_s / vsnprintf_s** (both behave the same; `errno` never changes): | Case | Payload process, firmware 13.60 | `main` before this PR | here now | |---|---|---|---| | `RSIZE_MAX` | 2^61 - 1 (a boundary search: `0x1fffffffffffffff` passes, `0x2000000000000000` fails) | `SIZE_MAX >> 1` | 2^61 - 1 | | null buffer, `size` 0 or above `RSIZE_MAX` | -1, buffer untouched, handler called with `"snprintf_s: bad argument"` (or `vsnprintf_s: …`), a null pointer and 22 | -1, no handler, `SIZE_MAX >> 1` accepted | as on the console | | null format | -1, `buffer[0]` = 0, handler `"…: bad format argument"` | -1, `buffer[0]` = 0, no handler | as on the console | | `%n` / `%hn` (also after truncation) | **-23**; the output before it stays written without a terminator, then `buffer[0]` = 0; handler `"printf_s: %n disallowed"`; the pointer is not written | -1, terminated output | as on the console | | null `%s` argument (also `%.0s`) | -23, same buffer handling, handler `"printf_s: bad %s argument"` | -1, no handler | as on the console | | null `%ls` / `%S` argument | -23, handler `"printf_s: bad %ls argument"` | -1, no handler | as on the console | **sscanf_s / vsscanf_s** (both behave the same): | Case | Payload process, firmware 13.60 | `main` before this PR | here now | |---|---|---|---| | size argument | 64 bits: a size of 2^32 + 2 is used as such | read as `unsigned int` | 64 bits | | `%s` / `%[` field of `size` or more characters | the first `size` characters stored without a terminator; matching failure, returns the items assigned so far | `buffer[0]` = 0 | as on the console | | `%c` with a width above `size` | stores `size` characters and succeeds, consuming only those | failure, `buffer[0]` = 0 | as on the console | | `%ls` / `%l[` and `%lc` with a width above `size` (titles' own `libc.prx`, measured by JJenkx) | `%ls` and `%l[` follow the narrow rules; `%lc` stores `size` units and succeeds, but consumes the whole width | `%lc` consumed only the stored characters | as measured | | size above `RSIZE_MAX` | handler `"scanf_s: bad %c, %s, or %[ size"` | not checked | as on the console | | null `%s` / `%c` / `%[` destination | handler `"scanf_s: bad %c, %s, or %[ argument"`, checked before reading input (also on empty input) | `EOF`, no handler | as on the console | | null integer, `%p` or floating-point destination | checked only after a successful conversion: empty input gives `EOF` and `"x"` gives 0, without the handler | `EOF` | as on the console | | result of any violation | the handler with the console's message, a null pointer and 22; returns the assignments so far, or **-23** when no conversion (suppressed ones and `%n` included) has completed | `EOF` | as on the console | | null input or format | `EOF`, handler `"sscanf_s: bad argument"` / `"vsscanf_s: bad argument"` | `EOF`, no handler | as on the console | | literal mismatch at the end of input (`""` with `"x%d"`) | 0 | `EOF` | 0 | **fscanf_s / vfscanf_s / scanf_s:** a literal mismatch at the end of input now returns the assignments, as `sscanf_s` does: empty input with `"x%2c%n"` returns 0 and leaves `errno`, the `%n` target and the destination untouched, following JJenkx's CPU-only run of two reference `libc` modules (earlier heads and `main` return `EOF`). `%%` at the end of input still returns `EOF`. A `%c` field takes the same width rule and size clamp as in `sscanf_s`: `%8c%n` on `abc` with size 2 stores `ab`, sets the count to 2 and leaves `c` next; with size 4 it stores `abc`; on `abcdef` with size 4 it stores `abcd` and leaves `e` next (JJenkx's reference rows; `main` returned 0 before reading). The stream forms were not run on a console. The -23 is the same with a recording handler, an empty one and `ignore_handler_s`. `set_constraint_handler_s`, `abort_handler_s` and `ignore_handler_s` are exported, and passing NULL reinstalls the default. **The formatter's terminator:** to leave the partial output unterminated on a violation, `FormatOutput` now writes the terminator once at the end instead of after every piece. Every caller goes through `FormatWindows`, which does that at the end. On success every output is the same as before, which the existing formatting tests check. **Not measured, recorded in `docs/dev/debt/libc.md`:** - The console's default handler writes `"<message>\n"` before ending the process; AnyPS5's default now writes the message and returns, as the `libc.prx` titles ship does (#9935's change, which reached `main` with #10270). - An unknown conversion such as `%y` is copied to the output by the console and still throws here. - A null `%hs` argument was not measured. - `swscanf_s`'s size width (it still reads `unsigned int`) was not measured. **Tests:** `guest_formatting`, `guest_scan_count` (also JJenkx's wide rows) and `guest_strings` check the measured cases, with the old expectations updated and size arguments passed as `size_t`. `guest_scan_count` fails if the -23 constant is changed. #### How the console was measured - **Setup:** - The console runs firmware 13.60 and is jailbroken. - The probes are ELF payloads built with [ps5-payload-dev/sdk](https://github.com/ps5-payload-dev/sdk) v0.44 and clang 18.1.8 and started through elfldr, so they ran in a payload process, not in a title. - **Provenance:** `libSceLibcInternal.sprx` was loaded with `dlopen`, and every function was taken from it with `dlsym`. - **Method:** - Buffers are filled with `~` and integer destinations preset to -7. - A recording constraint handler logs the message, pointer and error of each call. - `libcfmt3` and `libcfmt4` reinstall the default handler and end their own process with it, to see what it does. - **Files:** the probe sources and full logs are in [this gist](https://gist.github.com/orezraey/bf5de7486ef6941fe4a730f1cb4965ba). <details><summary>Console output (excerpt, copied unchanged)</summary> ``` libcfmt: meta fw=0x13600007 pid=153 sdk=v0.44 compiler=Clang 18.1.8 built=Oct 21 2026 08:20:42 log=/data/anyps5-probes/libcfmt.log libcfmt: case snprintf_s truncate size=5 "abcdef" ret=6 errno=0 buf="abcd\x00~~~~~~~~~~~" handler=0 libcfmt: case snprintf_s size=0 ret=-1 errno=0 buf="~~~~~~~~~~~~~~~~" handler=1 msg="snprintf_s: bad argument" err=22 ptr_off=-2 libcfmt: case size_boundary last_ret_1=0x1fffffffffffffff first_ret_-1=0x2000000000000000 libcfmt: case snprintf_s %n ret=-23 errno=0 buf="\x00b~~~~~~~~~~~~~~" handler=1 msg="printf_s: %n disallowed" err=22 ptr_off=-2 libcfmt: case vsscanf_s "abc1" %[a-z] cap=4 ret=1 errno=0 n1=-7 n2=-7 a="abc\x00~~~~" b="~~~~~~~~" handler=0 libcfmt: case vsscanf_s "abcd1" %[a-z] cap=4 ret=0 errno=0 n1=-7 n2=-7 a="abcd~~~~" b="~~~~~~~~" handler=0 libcfmt: case vsscanf_s "x" %3c cap=2 ret=1 errno=0 n1=-7 n2=-7 a="x~~~~~~~" b="~~~~~~~~" handler=0 libcfmt2: case vsscanf_s null %s first ret=-23 n1=-7 n2=-7 f=-7 a="~~~~~~~~" b="~~~~~~~~" handler=1 msg="scanf_s: bad %c, %s, or %[ argument" err=22 libcfmt2: case vsscanf_s "ab" %s cap=2^61 ret=-23 n1=-7 n2=-7 f=-7 a="~~~~~~~~" b="~~~~~~~~" handler=1 msg="scanf_s: bad %c, %s, or %[ size" err=22 libcfmt3: case vsscanf_s "5 ab" %d %s(null) %d ret=1 n1=5 n2=-7 f=-7 a="~~~~~~~~" b="~~~~~~~~" handler=1 msg="scanf_s: bad %c, %s, or %[ argument" err=22 ``` </details> ### Tested - Head `1c3b118df8`: `main` (`0263899e99`, with #13119) was merged in. `Formatting.cpp` met in two places: the `%n` bookkeeping keeps both #13119's ordinary rule and this PR's secure contiguous one, and `sscanf` uses #13119's `ScansInProcess` with this PR's null secure name. As agreed with JJenkx, the shared stream `%c` loop now takes a `%c` width by its low 32 bits (zero or above `INT_MAX` reads one) and clamps a secure field to its size instead of failing before reading; `guest_stdio_variadic` gains five stream rows from JJenkx's matrix (`%8c%n` on `abc` with sizes 2 and 4, on `abcdef` with size 4, `%2147483648c` and `%4294967298c`), which fail when the clamp is removed. `tests/Formatting.cpp` keeps this PR's unterminated `%s` check and #13119's `CheckOrdinaryCharacterScans`; `libc.md` keeps this PR's entries, and #13119's sentence about the secure `%c` width and size is dropped since both paths now follow those references. At this head `guest_formatting`, `guest_scan_count`, `guest_stdio_variadic`, `file_streams` and `guest_strings` pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL, GCC); `check_conventions` reports no errors (one `debt-length` warning on a `libc.md` entry this PR did not add). - Head `d44f653788`: `main` (`b8efa6292a`, with #11907) was merged in. Only `core/libs/tests/Formatting.cpp` met, in `CheckSecureFormat`: it keeps this PR's measured `snprintf_s`/`vsnprintf_s` checks and adds `main`'s new cases, `[%y][%-4k]` printed literally (with no constraint call) and `%hhn` refused with the buffer emptied and the destination untouched. At this head `guest_formatting`, `guest_wide_formatting`, `guest_scan_count`, `guest_stdio_variadic` and `guest_strings` pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL, GCC 16.2), and `check_conventions` reports 0 errors. - Head `0f9bb22b03`: `main` (`c2eafe4461`, with #9863 and #10270) was merged in. Only `libc.md` met: it keeps `main`'s `wcsncat_s` and `set_constraint_handler_s` entries (the default handler now writes the message and returns), #9863's short-field wording in the `_WStoul`/`swscanf_s` entry with this PR's measured `sscanf_s` comparison, and this PR's `snwprintf_s` sentence. `Formatting.cpp` now uses `SonyRsizeMax` from `SonyCrt.hpp` (same 2^61 - 1) instead of its own `secureSizeMax`, as agreed with harisibrar1 on this PR. At this head `guest_formatting`, `guest_scan_count`, `guest_stdio_variadic`, `guest_strings` and `guest_safe_errors` pass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL, GCC 16.2); `check_conventions` reports no errors (one `debt-length` warning on the long `_WStoul` entry). - Head `d826fd16d3`: `main` was merged in, with #12428; the unknown-conversion exit now comes before the secure block. `guest_formatting`, `guest_wide_formatting`, `guest_scan_count`, `guest_stdio_variadic`, `guest_safe_errors` and `guest_strings` pass three runs in a row on Windows (MinGW, GCC 15.2) and on Linux (WSL, GCC); `check_conventions` passes. Disregard the Linux result recorded for `24f4242cbd`: another checkout changed the shared tree while that build ran. This Linux run replaces it, on a tree nothing else touched. - Head `24f4242cbd`: `main` (`1d91f2966d`, with #12468) was merged in and the secure bounds were re-applied on its `source` scanner: for `sscanf_s` and `vsscanf_s`, `%ls`, `%lc` and `%l[` keep the first `size` characters without a terminator when they do not fit, and `%lc` consumes the rest of its width, which now counts bytes as #12468's loop does. Stream forms keep `main`'s behaviour. `libc.md` keeps #12468's `scanf_s` wording and this PR's. At this head `guest_formatting`, `guest_scan_count`, `guest_wide_formatting`, `guest_stdio_variadic`, `guest_strings`, `guest_wide_strings` and `guest_safe_errors` pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` reports no errors. - Head `e16c92d39f` merges `main` (`93dbd0f82a`, with #8397); only the `sscanf_s` debt line met #8397's new `_LDnorm` entry, and both are kept. At this head the twelve tests below pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL, GCC 16.2), and `check_conventions` reports no errors (one `debt-length` warning on a `libc.md` entry this PR did not add). - Head `053ebd8a33`: `main` (`1ff6c8739b`) was merged in. Only `libc.md` met, at the `scanf_s` and `freopen_s` entries: it keeps this PR's `scanf_s` wording with JJenkx's end-of-input result and `main`'s new `freopen_s` entry with its constraint handler calls. At this head `guest_formatting`, `guest_scan_count`, `guest_wide_formatting`, `guest_stdio_variadic`, `guest_strings` and `guest_safe_errors` pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` passes (the one `debt-length` warning on the `_WStoul` entry, as before). - Head `29d8de386f`: `main` (`3b32cc7b61`, with #12420) was merged in. Only `libc.md` met, in the `_WStoul`/`swscanf_s` entry: it keeps #12420's scanset range, length and `%p` wording and this PR's measured `sscanf_s` comparison (64-bit size, first `size` characters stored without a terminator). At this head `guest_formatting`, `guest_scan_count`, `guest_wide_formatting`, `guest_stdio_variadic`, `guest_strings` and `guest_safe_errors` pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` passes (the one `debt-length` warning on the `_WStoul` entry, as before). - Head `d00ac4ec91`: `main` (`0362b1521c`, with #12933) was merged in. Only `libc.md` met: it keeps #12933's new `dprintf`/`vdprintf` entry and this PR's measured `snprintf_s`/`vsnprintf_s` entry, with the old `vsscanf_s` entry still removed. At this head `guest_formatting`, `guest_scan_count`, `guest_wide_formatting`, `guest_stdio_variadic`, `guest_strings` and `guest_safe_errors` pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` passes (the one `debt-length` warning on the `_WStoul` entry, as before). - Head `4eaf64fee1`: `sscanf_s`/`vsscanf_s` take a `%c` width by its low 32 bits and read one character when it is zero or above `INT_MAX`, following JJenkx's reference rows (`%2147483648c` on `abc` stores one byte with size 2 or 4; `%4294967298c` reads two). `guest_scan_count` gains those three rows, which fail on `16a242bbaa`. The stream scanner keeps `main`'s path until #13057 and #13119 land. At this head `guest_formatting`, `guest_scan_count`, `guest_wide_formatting`, `guest_stdio_variadic`, `guest_strings` and `guest_safe_errors` pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` passes (the one `debt-length` warning on the `_WStoul` entry, as before). - Head `16a242bbaa`: the stream scanner (`fscanf_s`, `vfscanf_s`, `scanf_s`) returns the assignments for a literal mismatch at the end of input, following JJenkx's reference rows. `guest_stdio_variadic` gains the empty-input `"x%2c%n"` case (0, `errno` 97, `%n` -77 and the 0xa5 destination untouched) and `%%` at the end of file (`EOF`); the first check fails on the previous head. The twelve tests listed below pass three runs in a row on Windows (WinLibs GCC) and on Linux (WSL Ubuntu, GCC); `check_conventions` passes (the existing `_WStoul` `debt-length` warning only). - Head `3b0fa2fc4d`: `main` (`157b02c4f1`, with #11852) was merged in. Only `Formatting.cpp` met, in `sscanf`: it keeps #11852's cut at a `q` length directive (applied to `sscanf` and `vsscanf` only, as on `main`) and this PR's `ScanGuest` signature with a null secure name. At this head `guest_formatting`, `guest_scan_count`, `guest_stdio_variadic`, `guest_strings` and `guest_safe_errors` pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` passes (the one `debt-length` warning on the `_WStoul` entry, as before). - Head `ec862a495a`: `%lc` with a width above `size` consumes the whole width, following JJenkx's run of titles' `libc.prx` (PPSA07923, PPSA05916). `guest_scan_count` gains `CheckWideFields` with the eight rows of that table, and its two `%lc` rows fail on `acb1ec494f`. At this head `guest_formatting`, `guest_scan_count`, `guest_stdio_variadic`, `guest_strings` and `guest_safe_errors` pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` passes (the one `debt-length` warning on the `_WStoul` entry, as before). - Head `acb1ec494f`: `main` (`f80ce6cec1`, with #11121) was merged in. Only `libc.md` met: it keeps this PR's `snprintf_s`/`vsnprintf_s` and `set_constraint_handler_s` wording and its removal of the old `vsscanf_s` entry, and main's `localeconv` entry with its removal of the `setlocale` entry. At this head `guest_formatting`, `guest_scan_count`, `guest_stdio_variadic`, `guest_strings` and `guest_safe_errors` pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` passes (the one `debt-length` warning on the `_WStoul` entry, as before). - Head `0055557985`: `main` (`5bdda3661e`, with #10176 and #9973) was merged in. Only `libc.md` met: it keeps this PR's measured `sscanf_s`/`vsscanf_s` and `_WStoul`/`swscanf_s` entries, `main`'s firmware 13.60 export note on `memmem and memccpy` and #9973's new `floating scan errno` entry. At this head the twelve tests below pass three runs in a row on Windows (WinLibs GCC 15.2) and on Linux (WSL2, GCC); `check_conventions` passes (the one `debt-length` warning on the `_WStoul` entry, as before). - Head `2f05aac2d2`: `main` (`5b85225ef3`, with #9527's `ScanSource`) was merged in `5652af2d1a`. This PR's measured `sscanf_s`/`vsscanf_s` rules were ported onto `ScanSource` for `StringSource` only; `StreamSource` keeps `main`'s `fscanf_s`/`scanf_s` path. `2f05aac2d2` makes #9527's `GuestStdioVariadic` checks expect the measured `snprintf_s` (-23 with a handler call) and undersized `sscanf_s` `%s` results instead of the old ones. At this head the twelve tests below pass three runs in a row on Windows and on Linux, and the 49 libc tests matching `scan|format|print|string|stdio|safe|constraint|bounds|crt|locale|wide` pass on Windows. Before that, head after merging `main` again (`9dbc017c71`, with #9479): `efeb582327`; only `core/libs/tests/Formatting.cpp` met, keeping this PR's constraint-handler helpers and measured `sscanf_s` too-small `%s` check together with #9479's `CheckIntegerScanWidths` and its `RunChecks` call. At this head the twelve tests below pass three runs in a row on Windows and on Linux (the Linux `public_headers` audit times out at 30 s on the WSL `/mnt/c` checkout, unrelated to this change). Before that, `main` (`ac65cf490e`, with #9613) was merged in `b824e3b277`; only the `strndup` and `snprintf_s` debt lines met, keeping #9613's `strndup` disassembly reading and this PR's measured `snprintf_s` entry (the old `vsscanf_s` line stays removed, as its measured `sscanf_s` entry replaces it). At this head the twelve tests below pass three runs in a row on Windows and on Linux. Before that, `main` (`66e6772ca2`, with #9360) was merged in `6b9e2f1574`; only the `snwprintf_s` and `swprintf_s` debt lines met, keeping this PR's `snwprintf_s` wording and `main`'s `swprintf_s` limit. Before that, `main` (`f26434afa3`, with #10706) was merged in `7b400cbcfe`. The tests take their declarations from `libc/include/Api.hpp`. `guest_formatting`, `guest_scan_count`, `guest_wide_formatting`, `guest_wide_stream_scan`, `guest_stdio_variadic`, `guest_locale`, `guest_strings`, `guest_wide_strings`, `guest_wide_conversion`, `guest_safe_errors`, `guest_crt` and `guest_bounds_checking` pass three runs in a row on Windows (WinLibs GCC) and on Linux (WSL Ubuntu, GCC), which now also builds the `#ifndef _WIN32` wide scan path. `public_headers` audit and `check_conventions` pass (one `debt-length` warning on the `_WStoul` entry, already 3083 characters on `main`). Windows 11, WinLibs GCC 15.2.0, Release: the 30 libc tests whose names cover formatting, scanning, strings, wide characters, streams, locales and bounds checking pass. **Linux not built:** the `#ifndef _WIN32` wide scan path in `ScanGuest` was changed with the narrow rules and only passed a `g++` syntax check, so this PR relies on the Linux CI job to build and run it. ### 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) - [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: #7081, #7386, #9340 edit the same lines (merge order only); #9935's default constraint handler, which writes the message and returns so these new handler calls cannot end a title, reached `main` with #10270; #7851 (hypermoist) adds `libc.md` entries next to the ones this PR edits (merge order only); #12574 (JJenkx) edits `libc.md` lines this PR changes (merge order only); #12627 (JJenkx) rewrites the `snwprintf_s` and `swprintf_s` entries this PR edits in `libc.md` (merge order only); #12598 (JJenkx) rewrites the `qsort_s` entry next to the `libc.md` entries this PR edits (merge order only); #9688, #9863, #10729, #11621, #11907, #12397, #12468, #12530, #12532 and #13119 have merged - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
MarcZX10 commented 2026-10-21 18:36:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T18:36:44Z.


Overlap coordination for a follow-up to #9321: I have a narrow wide-scanner patch for incomplete-exponent and literal-at-EOF behavior. Its code and tests are separate from this PR's measured formatting constraints, but both branches update the same libc debt bullet, which pr_overlap.py flags. Would you prefer that follow-up to depend on this PR and rebase the debt update after yours merges, or do you have another sequencing preference?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6323642670 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9087#issuecomment-6323642670). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T18:36:44Z. --- Overlap coordination for a follow-up to #9321: I have a narrow wide-scanner patch for incomplete-exponent and literal-at-EOF behavior. Its code and tests are separate from this PR's measured formatting constraints, but both branches update the same libc debt bullet, which `pr_overlap.py` flags. Would you prefer that follow-up to depend on this PR and rebase the debt update after yours merges, or do you have another sequencing preference?
orezraey commented 2026-10-21 19:33:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-21T19:33:48Z.


Thanks for checking. Making your follow-up depend on this PR and rebasing the debt bullet after it merges works for me. If this one is still waiting when yours is ready, go ahead first and I'll rebase the debt bullet here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6324778158 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9087#issuecomment-6324778158). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-21T19:33:48Z. --- Thanks for checking. Making your follow-up depend on this PR and rebasing the debt bullet after it merges works for me. If this one is still waiting when yours is ready, go ahead first and I'll rebase the debt bullet here.
EagleBearerYS commented 2026-10-22 03:43:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-22T03:43:37Z.


@orezraey a merge-order note: #9479 (Windows scanf integer conversions) adds a check function after CheckWidePrecision in core/libs/tests/Formatting.cpp and a call at the end of RunChecks, where this PR adds its own, so the two conflict in that test file only. The library changes are in different files: this PR's in Formatting.cpp and WindowsFormatting.hpp, #9479's in WindowsScanning.hpp. MarcZX10's code review on #9479 is resolved, and MarcZX10 asked that the two agree on the order (comment). #9479 proposes going first, and this PR would rebase the test file over it, keeping both check functions and both calls. If you'd rather this one went first, say so and #9479 will list it under Depends on and do the rebase itself.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6332145417 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9087#issuecomment-6332145417). Originally posted by [EagleBearerYS](https://github.com/EagleBearerYS) on 2026-10-22T03:43:37Z. --- @orezraey a merge-order note: #9479 (Windows `scanf` integer conversions) adds a check function after `CheckWidePrecision` in `core/libs/tests/Formatting.cpp` and a call at the end of `RunChecks`, where this PR adds its own, so the two conflict in that test file only. The library changes are in different files: this PR's in `Formatting.cpp` and `WindowsFormatting.hpp`, #9479's in `WindowsScanning.hpp`. MarcZX10's code review on #9479 is resolved, and MarcZX10 asked that the two agree on the order ([comment](https://github.com/boykopovar/AnyPS5/pull/9479#issuecomment-6332072954)). #9479 proposes going first, and this PR would rebase the test file over it, keeping both check functions and both calls. If you'd rather this one went first, say so and #9479 will list it under Depends on and do the rebase itself.
orezraey commented 2026-10-22 03:50:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-22T03:50:34Z.


Thanks for coordinating. #9479 going first works for me; once it merges I'll rebase this PR's test file over it, keeping both check functions and both calls in RunChecks.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6332231843 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9087#issuecomment-6332231843). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-22T03:50:34Z. --- Thanks for coordinating. #9479 going first works for me; once it merges I'll rebase this PR's test file over it, keeping both check functions and both calls in `RunChecks`.
JJenkx commented 2026-10-22 04:28:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T04:28:12Z.


A data point for the set_constraint_handler_s debt line, from a different module than the one it cites. Your text describes libSceLibcInternal.sprx (firmware 13.60), whose default handler writes the message and a newline, then ends the process.

The libc.prx that PPSA13934 ships in sce_module/ behaves differently: its default handler returns. I read this from disassembly, not by measuring.

  • The default handler is installed at load and when NULL is passed. set_constraint_handler_s (ENLfKJEZTjE, 0x4360) stores handler ? handler : 0x43b0. The slot's initial value is an R_X86_64_RELATIVE relocation with addend 0x43b0. That is neither abort_handler_s (0x43e0) nor ignore_handler_s (0x4410).
  • 0x43b0, the default handler, writes the message and a newline, then returns:
    lea    _Stderr,%rbx          ; H8AprKeZtNg, 0x110480
    mov    %rbx,%rsi
    call   fputs                 ; QrZZdJ8XsX0, 0x44a0 (message in rdi)
    mov    $0xa,%edi
    mov    %rbx,%rsi
    call   fputc                 ; aZK8lNei-Qw, 0x4730
    add    $0x8,%rsp
    pop    %rbx
    pop    %rbp
    ret
    
  • abort_handler_s (0x43e0) makes the same two calls, then calls abort (L1SBTkC+Cvw, 0x48d0).

What this means: sources disagree on whether the default handler ends the process. For a title that installs no handler, this bundled libc.prx writes the line to stderr and lets the _s function return its error. That matches the wcsrtombs_s debt line ("writes the message and a newline to stderr before returning").

main's SonyDefaultConstraintHandler calls std::abort() without writing anything. The copies of PPSA07923 and PPSA05916 have the same default handler: fputs and fputc to _Stderr, then ret.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6332707345 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9087#issuecomment-6332707345). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T04:28:12Z. --- A data point for the `set_constraint_handler_s` debt line, from a different module than the one it cites. Your text describes `libSceLibcInternal.sprx` (firmware 13.60), whose default handler writes the message and a newline, then ends the process. The `libc.prx` that PPSA13934 ships in `sce_module/` behaves differently: its default handler **returns**. I read this from disassembly, not by measuring. - **The default handler is installed at load and when `NULL` is passed.** `set_constraint_handler_s` (`ENLfKJEZTjE`, 0x4360) stores `handler ? handler : 0x43b0`. The slot's initial value is an `R_X86_64_RELATIVE` relocation with addend 0x43b0. That is neither `abort_handler_s` (0x43e0) nor `ignore_handler_s` (0x4410). - **0x43b0, the default handler, writes the message and a newline, then returns:** ``` lea _Stderr,%rbx ; H8AprKeZtNg, 0x110480 mov %rbx,%rsi call fputs ; QrZZdJ8XsX0, 0x44a0 (message in rdi) mov $0xa,%edi mov %rbx,%rsi call fputc ; aZK8lNei-Qw, 0x4730 add $0x8,%rsp pop %rbx pop %rbp ret ``` - **`abort_handler_s` (0x43e0) makes the same two calls, then calls `abort` (`L1SBTkC+Cvw`, 0x48d0).** **What this means:** sources disagree on whether the default handler ends the process. For a title that installs no handler, this bundled `libc.prx` writes the line to `stderr` and lets the `_s` function return its error. That matches the `wcsrtombs_s` debt line ("writes the message and a newline to `stderr` before returning"). `main`'s `SonyDefaultConstraintHandler` calls `std::abort()` without writing anything. The copies of PPSA07923 and PPSA05916 have the same default handler: `fputs` and `fputc` to `_Stderr`, then `ret`.
harisibrar1 commented 2026-10-24 13:28:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:28:42Z.

Review: APPROVED


Checked against main 6cd36e6517 and the libcfmt/libcfmt2/libcfmt3/libcfmt4 logs and probe sources in the linked gist:

  • I traced every logged snprintf_s/vsnprintf_s/vsscanf_s/sscanf_s case through FormatSecure, FormatOutput and ScanGuest at this head, and each matches the console's return value, buffer bytes and handler message (including %*s/%n counting as a completed conversion, the size check coming before the end-of-input check, and %n after truncation leaving "\0b"). The branch still merges cleanly with current main.
  • Nothing on main contradicts the measurements: main's BoundsChecking.cpp already uses 2^61 - 1, and #10270 and #8965 measured the same limit, EINVAL and a null handler pointer.
  • Small follow-up: snwprintf_s (FormattingWide.cpp:247, and its debt line saying RSIZE_MAX is assumed to be SIZE_MAX >> 1) will now disagree with snprintf_s on the size limit. Could the debt line mention it, or could it get its own PR?
  • #10270 adds SonyRsizeMax to SonyCrt.hpp, which this file already includes, so whichever lands second could reuse it instead of defining secureSizeMax again.
  • Minor, in the gist: the libcfmt3 case labelled "x" %s cap=SIZE_MAX actually runs "%*s %s" (libcfmt3_main.c:158), which is why it returns 0 rather than -23. Fixing the label would help later readers.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638414124 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9087#pullrequestreview-5638414124). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:28:42Z. **Review:** APPROVED --- Checked against main 6cd36e6517 and the libcfmt/libcfmt2/libcfmt3/libcfmt4 logs and probe sources in the linked gist: - I traced every logged `snprintf_s`/`vsnprintf_s`/`vsscanf_s`/`sscanf_s` case through `FormatSecure`, `FormatOutput` and `ScanGuest` at this head, and each matches the console's return value, buffer bytes and handler message (including `%*s`/`%n` counting as a completed conversion, the size check coming before the end-of-input check, and `%n` after truncation leaving `"\0b"`). The branch still merges cleanly with current main. - Nothing on main contradicts the measurements: main's `BoundsChecking.cpp` already uses 2^61 - 1, and #10270 and #8965 measured the same limit, `EINVAL` and a null handler pointer. - Small follow-up: `snwprintf_s` (`FormattingWide.cpp:247`, and its debt line saying `RSIZE_MAX` is assumed to be `SIZE_MAX >> 1`) will now disagree with `snprintf_s` on the size limit. Could the debt line mention it, or could it get its own PR? - #10270 adds `SonyRsizeMax` to `SonyCrt.hpp`, which this file already includes, so whichever lands second could reuse it instead of defining `secureSizeMax` again. - Minor, in the gist: the libcfmt3 case labelled `"x" %s cap=SIZE_MAX` actually runs `"%*s %s"` (libcfmt3_main.c:158), which is why it returns 0 rather than -23. Fixing the label would help later readers. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-11-20 08:10:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T08:10:23Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this draft review; one optional consistency point is noted below.

What I ran

Reviewed head 16498b1a6a3393138771095c3d0c487f75098e2c using repo_read.py. No commands or tests were run; this review interface is read-only.

Read all seven changed files listed in manifest.json: WindowsFormatting.hpp, Formatting.cpp, Formatting.cpp tests, GuestScanCount.cpp, GuestStdioVariadic.cpp, GuestStrings.cpp, and libc.md. All were marked included. No exclusions were listed in the manifest; files.json was not available through the reader.

Results

Check Result Note
Changed code review passed Seven changed files read
Tests not run Read-only review interface
Console behavior not run Stream forms unmeasured on-console

Findings

Blocking

None.

Non-blocking

  • The secure stream wide %lc branch keeps the full parsed width and returns early when it exceeds the destination capacity (Formatting.cpp:431); unlike the narrow stream %c branch, it does not apply the low-32-bit width rule and clamp to capacity. The added wide-field tests cover sscanf_s, not the stream variant. Since the description explicitly says stream forms were not run on-console, consider adding a reference-based stream case or recording this as unverified if parity is intended.

Not verified

  • No build or tests were run.
  • PS5 console behavior was not run during this review. The description reports console measurements for formatting and string scanning, and contributor-run reference-module observations for stream scanning; those are author-reported evidence.
  • files.json could not be read through the provided reader.

To get this to a good state

Nothing is required. Optional: add a stream %lc width/capacity case or clarify the intended distinction from the narrow stream %c behavior.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5989416548 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9087#pullrequestreview-5989416548). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T08:10:23Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this draft review; one optional consistency point is noted below. ## What I ran Reviewed head `16498b1a6a3393138771095c3d0c487f75098e2c` using `repo_read.py`. No commands or tests were run; this review interface is read-only. Read all seven changed files listed in `manifest.json`: `WindowsFormatting.hpp`, `Formatting.cpp`, `Formatting.cpp` tests, `GuestScanCount.cpp`, `GuestStdioVariadic.cpp`, `GuestStrings.cpp`, and `libc.md`. All were marked included. No exclusions were listed in the manifest; `files.json` was not available through the reader. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | Seven changed files read | | Tests | not run | Read-only review interface | | Console behavior | not run | Stream forms unmeasured on-console | ## Findings ### Blocking None. ### Non-blocking - The secure stream wide `%lc` branch keeps the full parsed width and returns early when it exceeds the destination capacity (`Formatting.cpp:431`); unlike the narrow stream `%c` branch, it does not apply the low-32-bit width rule and clamp to capacity. The added wide-field tests cover `sscanf_s`, not the stream variant. Since the description explicitly says stream forms were not run on-console, consider adding a reference-based stream case or recording this as unverified if parity is intended. ## Not verified - No build or tests were run. - PS5 console behavior was not run during this review. The description reports console measurements for formatting and string scanning, and contributor-run reference-module observations for stream scanning; those are author-reported evidence. - `files.json` could not be read through the provided reader. ## To get this to a good state Nothing is required. Optional: add a stream `%lc` width/capacity case or clarify the intended distinction from the narrow stream `%c` behavior.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/9087:upstream-pr/9087'
git switch 'upstream-pr/9087'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/9087'
git switch 'upstream-pr/9087'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/9087'
git switch 'upstream-pr/9087'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/9087'
git switch 'main'
git merge --squash 'upstream-pr/9087'
git switch 'main'
git merge --ff-only 'upstream-pr/9087'
git switch 'main'
git merge 'upstream-pr/9087'
git push origin 'main'
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!9087
No description provided.