fix(libc): print %lp in the wide formatter as measured on a PS5 #11214

Merged
orezraey merged 5 commits from upstream-pr/11214 into main 2026-11-02 10:27:09 +00:00
orezraey commented 2026-10-25 21:56:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-25T21:56:01Z.


What

The wide formatter's _s functions (snwprintf_s, vswprintf_s, vfwprintf_s and the rest that pass secure = true) now print %lp as they print %p; until now they returned -1. Since #11893 the plain functions (swprintf, vswprintf) already drop the length on %p before this branch. The system libSceLibcInternal of a retail PS5 (firmware 13.60) prints %lp as %p through swprintf. No _s function was run with %lp on a console: they follow swprintf and the narrow _s forms here, and the debt entry says so. Measured with a homebrew probe from a payload process; the source and console log are in this gist (pfmtp).

On the console:

  • swprintf of [%lp] gives [1f] (return 4) for 0x1f, and [0] (return 3) for null, exactly as [%p] does. [%ls] of a mixed string prints it unchanged.

  • The cases the debt entry listed as unmeasured, through snprintf:

    • a width beside %.0p: %5.0p of null gives five spaces;
    • the 0 flag beside a precision: %05.3p of 0x1f gives 01f;
    • %.0p of a non-null pointer gives 1f.

    -, #, %20p and the null %p give 1f , 1f, 18 spaces then 1f, and 0. That is what AnyPS5's shared pointer formatter already prints.

Another length on %p, such as %hp, follows #11893 (merged), which ignores it in the plain forms as titles' libc.prx does. The debt entry now records the measured cases.

Before and after

Format PS5 AnyPS5 before AnyPS5 now
swprintf [%lp] of 0x1f [1f], returns 4 [1f], returns 4 since #11893 the same
swprintf [%lp] of null [0], returns 3 [0], returns 3 since #11893 the same
snwprintf_s [%lp] of 0x1f not measured returns -1 [1f], returns 4
snwprintf_s [%lp] of null not measured returns -1 [0], returns 3
%5.0p, %05.3p, %.0p and the other %p flags as listed above the same the same
%hp and other lengths not measured ignored since #11893 ignored since #11893

Tested

Windows 11, WinLibs GCC 15.2. The 13 tests matching format pass 3 times out of 3. guest_wide_formatting now checks:

  • [%lp] gives [1234] (6) and [0] (3);
  • [%5.0p][%05.3p][%.0p] gives [ ][ 01f][1f] (18).

tools/check_conventions.py passes. Head e2bbf70239 on main 88f99678bf. After merging main (2cce8bc51d, which brought #9927), head 3315252 passes guest_wide_formatting 3 times out of 3 on Windows (GCC 15.2) and on Linux (WSL Ubuntu, GCC), and check_conventions reports no errors. After merging main (899ae3d1ad, which brought #11893 and its own %lp and %hp handling; the [%hp] returns -1 check was dropped and the debt entry points to the length modifiers entry), head 382120ba67 passes guest_formatting and guest_wide_formatting 3 times out of 3 on Windows (GCC 15.2) and on Linux (WSL Ubuntu, GCC), and check_conventions reports no errors. Head 246110e525 adds snwprintf_s %lp checks for 0x1f and null: guest_formatting and guest_wide_formatting pass 3 times out of 3 on Windows (GCC 15.2) and on Linux (WSL, GCC 16.2), run at 813cbd96bb; 246110e525 changes only the debt text. Without the FormattingWide.cpp change the new check fails with snwprintf_s %lp prints the pointer as %p does. check_conventions passes.

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: #5303 (its test expects swprintf("%lp") to fail, which this measurement contradicts), #9927, #9958 and #10379 (they edit the debt entries next to this one); merge order only; #11907 and #12397 (JJenkx) rewrite the same %lp check in GuestWideFormatting.cpp (merge order only)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5009104286 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11214). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-25T21:56:01Z. --- ### What The wide formatter's `_s` functions (`snwprintf_s`, `vswprintf_s`, `vfwprintf_s` and the rest that pass `secure = true`) now print `%lp` as they print `%p`; until now they returned -1. Since #11893 the plain functions (`swprintf`, `vswprintf`) already drop the length on `%p` before this branch. The system `libSceLibcInternal` of a retail PS5 (firmware 13.60) prints `%lp` as `%p` through `swprintf`. No `_s` function was run with `%lp` on a console: they follow `swprintf` and the narrow `_s` forms here, and the debt entry says so. Measured with a homebrew probe from a payload process; the source and console log are in [this gist](https://gist.github.com/orezraey/65fa9a319873b3b277d6b60954e843f7) (`pfmtp`). On the console: - `swprintf` of `[%lp]` gives `[1f]` (return 4) for `0x1f`, and `[0]` (return 3) for null, exactly as `[%p]` does. `[%ls]` of a mixed string prints it unchanged. - The cases the debt entry listed as unmeasured, through `snprintf`: - a width beside `%.0p`: `%5.0p` of null gives five spaces; - the `0` flag beside a precision: `%05.3p` of `0x1f` gives ` 01f`; - `%.0p` of a non-null pointer gives `1f`. `-`, `#`, `%20p` and the null `%p` give `1f `, `1f`, 18 spaces then `1f`, and `0`. That is what AnyPS5's shared pointer formatter already prints. Another length on `%p`, such as `%hp`, follows #11893 (merged), which ignores it in the plain forms as titles' `libc.prx` does. The debt entry now records the measured cases. **Before and after** | Format | PS5 | AnyPS5 before | AnyPS5 now | |---|---|---|---| | `swprintf` `[%lp]` of `0x1f` | `[1f]`, returns 4 | `[1f]`, returns 4 since #11893 | the same | | `swprintf` `[%lp]` of null | `[0]`, returns 3 | `[0]`, returns 3 since #11893 | the same | | `snwprintf_s` `[%lp]` of `0x1f` | not measured | returns -1 | `[1f]`, returns 4 | | `snwprintf_s` `[%lp]` of null | not measured | returns -1 | `[0]`, returns 3 | | `%5.0p`, `%05.3p`, `%.0p` and the other `%p` flags | as listed above | the same | the same | | `%hp` and other lengths | not measured | ignored since #11893 | ignored since #11893 | ### Tested <!-- 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. --> Windows 11, WinLibs GCC 15.2. The 13 tests matching `format` pass 3 times out of 3. `guest_wide_formatting` now checks: - `[%lp]` gives `[1234]` (6) and `[0]` (3); - `[%5.0p][%05.3p][%.0p]` gives `[ ][ 01f][1f]` (18). `tools/check_conventions.py` passes. Head `e2bbf70239` on `main` `88f99678bf`. After merging `main` (`2cce8bc51d`, which brought #9927), head `3315252` passes `guest_wide_formatting` 3 times out of 3 on Windows (GCC 15.2) and on Linux (WSL Ubuntu, GCC), and `check_conventions` reports no errors. After merging `main` (`899ae3d1ad`, which brought #11893 and its own `%lp` and `%hp` handling; the `[%hp]` returns -1 check was dropped and the debt entry points to the length modifiers entry), head `382120ba67` passes `guest_formatting` and `guest_wide_formatting` 3 times out of 3 on Windows (GCC 15.2) and on Linux (WSL Ubuntu, GCC), and `check_conventions` reports no errors. Head `246110e525` adds `snwprintf_s` `%lp` checks for `0x1f` and null: `guest_formatting` and `guest_wide_formatting` pass 3 times out of 3 on Windows (GCC 15.2) and on Linux (WSL, GCC 16.2), run at `813cbd96bb`; `246110e525` changes only the debt text. Without the `FormattingWide.cpp` change the new check fails with `snwprintf_s %lp prints the pointer as %p does`. `check_conventions` passes. ### 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: #5303 (its test expects `swprintf("%lp")` to fail, which this measurement contradicts), #9927, #9958 and #10379 (they edit the debt entries next to this one); merge order only; #11907 and #12397 (JJenkx) rewrite the same `%lp` check in `GuestWideFormatting.cpp` (merge order only) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
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!11214
No description provided.