fix(libc): ignore printf length modifiers that do not apply, as titles' libc.prx does #11893

Merged
JJenkx merged 2 commits from upstream-pr/11893 into main 2026-10-29 12:49:02 +00:00
JJenkx commented 2026-10-27 11:55:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-27T11:55:46Z.


What

The printf family throws when a length modifier does not apply to its conversion, which aborts the title. The wide family returns -1 instead. main throws "Invalid integer length" for %Ld, "Unsupported format conversion" for %hhc, %Ls, %Lc and %hp, and "Invalid floating length" for %hf or %zg.

The libc.prx that titles ship ignores such a modifier and formats the conversion as if it were absent:

Conversion Modifier ignored Titles' copy main
d i o u x X L (formats an int) %Ld of 0x89abcdef gives -1985229329 throws
n L (writes an int) a%Ln stores 1 in the low 32 bits throws; wide: -1
c, s anything but l %hhc%Lc%zc gives DEF, %hhs gives the narrow string throws
a e f g (and upper case) anything but L %hf, %llf give 1.500000, %ja gives 0x1.8p+0 throws; wide: -1
p any length %hp, %Lp give 1234 throws; wide: -1

Width, precision and flags still apply: [%5Ld][%-8.3Lx] gives [1094861636][41424344].

A new LibcDetail::ApplicableLength drops the modifier in FormatWindows and in the wide formatter. On Linux, RequiresGuestFormatting sends a format with such a modifier to the guest formatter, because glibc reads %Ld as long long. The _s forms are unmeasured and still throw; a new debt entry records that.

Tested

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, main 8ff4baf534.

  • Comparison: a scratch loader runs the decrypted libc.prx of PPSA07923 and PPSA13934 natively, with one process per format.
    • snprintf:
      • L on d i u x X o and n;
      • each of h hh l ll j z t L on c and s;
      • h hh ll j z t l L on f, and h or z on e, g and a;
      • h hh ll L z on p;
      • each length on n.
    • swprintf: %Ld %Lx %hf %zg %hp %Lp %hhc %Lc %hhs %Ls %hs %Ln.
    • The results are as in the table. Where a modifier applies (hh h l ll j z t on integers, l on c and s), the results match main. The unknown modifiers %qd, %I64d and %wd print as text on both.
    • Each expected string in the new tests was printed by both titles' snprintf and swprintf with the same arguments.
  • guest_formatting: the new CheckIgnoredLengths covers every row of the table.
  • guest_wide_formatting:
    • %Ln now expects the count written as an int, as the titles' swprintf does; before, it expected -1.
    • A new check covers %Ld|%Lx|%hf|%zg|%hp|%Lc.
    • [%lp], which an existing check expected to be refused, now expects [1234]. The titles' swprintf prints %lp, %llp and %hp as 1234.
  • guest_formatting, guest_wide_formatting and guest_stdio_variadic pass in three runs out of three on the exact head 17c950f0a5. That commit passes int arguments to %Ld and an int with a guard to %Ln, per review; the first head 4630a53d0e also passed 3/3.
  • Linux: 3/3 for guest_formatting and guest_wide_formatting at 4630a53d0e (comment), and MarcZX10's 3/3 of the three groups at #11907's head 15b33e8e76, whose parent is 17c950f0a5.
  • Negative control: with main's WindowsFormatting.hpp, Formatting.cpp and FormattingWide.cpp and these tests, guest_formatting and guest_wide_formatting abort (exit 0xc0000409).
  • python tools/check_conventions.py --base origin/main and git diff --check pass.
  • Not tested: a console, macOS, and the _s forms. %Lf with a double argument is mismatched arguments and is not covered.

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: none
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5033476141 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11893). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-27T11:55:46Z. --- ### What The printf family throws when a length modifier does not apply to its conversion, which aborts the title. The wide family returns -1 instead. `main` throws "Invalid integer length" for `%Ld`, "Unsupported format conversion" for `%hhc`, `%Ls`, `%Lc` and `%hp`, and "Invalid floating length" for `%hf` or `%zg`. The `libc.prx` that titles ship ignores such a modifier and formats the conversion as if it were absent: | Conversion | Modifier ignored | Titles' copy | `main` | |---|---|---|---| | `d i o u x X` | `L` (formats an `int`) | `%Ld` of `0x89abcdef` gives `-1985229329` | throws | | `n` | `L` (writes an `int`) | `a%Ln` stores 1 in the low 32 bits | throws; wide: -1 | | `c`, `s` | anything but `l` | `%hhc%Lc%zc` gives `DEF`, `%hhs` gives the narrow string | throws | | `a e f g` (and upper case) | anything but `L` | `%hf`, `%llf` give `1.500000`, `%ja` gives `0x1.8p+0` | throws; wide: -1 | | `p` | any length | `%hp`, `%Lp` give `1234` | throws; wide: -1 | Width, precision and flags still apply: `[%5Ld][%-8.3Lx]` gives `[1094861636][41424344]`. A new `LibcDetail::ApplicableLength` drops the modifier in `FormatWindows` and in the wide formatter. On Linux, `RequiresGuestFormatting` sends a format with such a modifier to the guest formatter, because glibc reads `%Ld` as `long long`. The `_s` forms are unmeasured and still throw; a new debt entry records that. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` 8ff4baf534. - **Comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923 and PPSA13934 natively, with one process per format. - **`snprintf`:** - `L` on `d i u x X o` and `n`; - each of `h hh l ll j z t L` on `c` and `s`; - `h hh ll j z t l L` on `f`, and `h` or `z` on `e`, `g` and `a`; - `h hh ll L z` on `p`; - each length on `n`. - **`swprintf`:** `%Ld %Lx %hf %zg %hp %Lp %hhc %Lc %hhs %Ls %hs %Ln`. - The results are as in the table. Where a modifier applies (`hh h l ll j z t` on integers, `l` on `c` and `s`), the results match `main`. The unknown modifiers `%qd`, `%I64d` and `%wd` print as text on both. - Each expected string in the new tests was printed by both titles' `snprintf` and `swprintf` with the same arguments. - **`guest_formatting`:** the new `CheckIgnoredLengths` covers every row of the table. - **`guest_wide_formatting`:** - `%Ln` now expects the count written as an `int`, as the titles' `swprintf` does; before, it expected -1. - A new check covers `%Ld|%Lx|%hf|%zg|%hp|%Lc`. - `[%lp]`, which an existing check expected to be refused, now expects `[1234]`. The titles' `swprintf` prints `%lp`, `%llp` and `%hp` as `1234`. - `guest_formatting`, `guest_wide_formatting` and `guest_stdio_variadic` pass in three runs out of three on the exact head `17c950f0a5`. That commit passes `int` arguments to `%Ld` and an `int` with a guard to `%Ln`, per review; the first head `4630a53d0e` also passed 3/3. - **Linux:** 3/3 for `guest_formatting` and `guest_wide_formatting` at `4630a53d0e` ([comment](https://github.com/boykopovar/AnyPS5/pull/11893#issuecomment-6441545171)), and MarcZX10's 3/3 of the three groups at #11907's head `15b33e8e76`, whose parent is `17c950f0a5`. - **Negative control:** with `main`'s `WindowsFormatting.hpp`, `Formatting.cpp` and `FormattingWide.cpp` and these tests, `guest_formatting` and `guest_wide_formatting` abort (exit 0xc0000409). - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - **Not tested:** a console, macOS, and the `_s` forms. `%Lf` with a `double` argument is mismatched arguments and is not covered. ### 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: none - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-27 12:02:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T12:02:01Z.

Review: DISMISSED


The formatting changes have useful title measurements and broad Windows coverage, but the new regression tests pass argument types that do not match the behavior the formatter now implements, so their results are undefined and may vary across compilers/ABIs.

  • In Formatting.cpp:81, %Ld is normalized to %d, and FormatWindows then retrieves an int (args.Next<int>() at WindowsFormatting.hpp:645). The test passes 0x89abcdefu, an unsigned int value not representable as int. Please pass an int value for %Ld (while keeping the %Lx/%Lu/%Lo arguments unsigned).
  • In Formatting.cpp:87-88, %Ln is normalized to %n, which retrieves and writes an int*; the test passes a long long* and reads the modified long long. Please use an int count with an adjacent guard field if the test needs to prove only the int bytes were written.

After correcting those cases, please run the focused formatting tests on Linux as well. Formatting.cpp changes Linux's RequiresGuestFormatting routing, and the PR currently says that path was not tested. The Windows title measurements do not exercise that routing.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5666710881 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11893#pullrequestreview-5666710881). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T12:02:01Z. **Review:** DISMISSED --- The formatting changes have useful title measurements and broad Windows coverage, but the new regression tests pass argument types that do not match the behavior the formatter now implements, so their results are undefined and may vary across compilers/ABIs. - In `Formatting.cpp:81`, `%Ld` is normalized to `%d`, and `FormatWindows` then retrieves an `int` (`args.Next<int>()` at `WindowsFormatting.hpp:645`). The test passes `0x89abcdefu`, an `unsigned int` value not representable as `int`. Please pass an `int` value for `%Ld` (while keeping the `%Lx/%Lu/%Lo` arguments unsigned). - In `Formatting.cpp:87-88`, `%Ln` is normalized to `%n`, which retrieves and writes an `int*`; the test passes a `long long*` and reads the modified `long long`. Please use an `int` count with an adjacent guard field if the test needs to prove only the `int` bytes were written. After correcting those cases, please run the focused formatting tests on Linux as well. `Formatting.cpp` changes Linux's `RequiresGuestFormatting` routing, and the PR currently says that path was not tested. The Windows title measurements do not exercise that routing.
MarcZX10 commented 2026-10-29 00:17:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T00:17:42Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

The test argument-type fixes are present, and #11907's descendant run covers this exact source head, but #11893 is still based on an older main.

What I ran

Reviewed head 17c950f0a54f8e44ec1e7561034ab6d7086ef824; its merge base with current main is 8ff4baf53489033dae79090640143e82d159ee4d. The current-main preview is clean, but docs/dev/debt/libc.md changed on both sides. I inspected the corrected %Ld and %Ln test changes. Environment: Linux x86-64, Git and GitHub CLI. I did not build or run tests locally.

git fetch https://github.com/boykopovar/AnyPS5.git main:refs/review/main-20261029 refs/pull/11893/head:refs/review/pr-11893-20261029
git merge-base refs/review/main-20261029 refs/review/pr-11893-20261029
git merge-tree --write-tree refs/review/main-20261029 refs/review/pr-11893-20261029
base=$(git merge-base refs/review/main-20261029 refs/review/pr-11893-20261029)
comm -12 <(git diff --name-only "$base" refs/review/main-20261029 | sort) <(git diff --name-only "$base" refs/review/pr-11893-20261029 | sort)

Results

Check Result
Current-main merge preview Clean
Touched paths changed on current main since merge base docs/dev/debt/libc.md
Test argument types The %Ld test passes an int; the %Ln test writes into guarded int storage
Focused formatting tests Author reports Linux 3/3 on this exact head; a descendant #11907 run also covers it
Title-side comparisons Author reports bundled-title comparisons for the documented formats
Local build/tests Not run
Command output
head=17c950f0a54f8e44ec1e7561034ab6d7086ef824
merge-base=8ff4baf53489033dae79090640143e82d159ee4d
merge-preview=52099122bfdefc19afa3ca84bfed047fcf8befdf
touched-path-intersection:
docs/dev/debt/libc.md

Negative control

Assertion Change to code under test Outcome
Corrected %Ld and %Ln tests validate the actual argument types and write width The PR changes only the test inputs/guard storage for these cases The tests now avoid the prior type mismatch and over-sized write; no separate base-source negative control was run here

Findings

Blocking

  • The branch is based on 8ff4baf534, not current main 3a2b91981651. The merge preview is clean, but the contribution rules require keeping the branch current. Rebase and rerun the focused tests on that base before approval.

Non-blocking

  • The requested test fixes are present. The reported #11907 test run uses this exact head as its parent, so a separate run of the old-base head is not needed; the post-rebase run remains necessary.

Sources

Not verified

  • I did not independently run formatting tests or compare against a console. The Windows-host title comparisons and Linux test results are author/reviewer-provided.
  • The current hosted Linux product job is skipped; it does not validate the Linux routing.

To get this to a good state

Rebase onto current main, preserve the updated debt wording, and rerun guest_formatting and guest_wide_formatting on the new head. Record the exact head/base; I will re-check and rerun the focused tests available here.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5691354909 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11893#pullrequestreview-5691354909). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-29T00:17:42Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested The test argument-type fixes are present, and #11907's descendant run covers this exact source head, but #11893 is still based on an older `main`. ## What I ran Reviewed head `17c950f0a54f8e44ec1e7561034ab6d7086ef824`; its merge base with current `main` is `8ff4baf53489033dae79090640143e82d159ee4d`. The current-main preview is clean, but `docs/dev/debt/libc.md` changed on both sides. I inspected the corrected `%Ld` and `%Ln` test changes. Environment: Linux x86-64, Git and GitHub CLI. I did not build or run tests locally. ```sh git fetch https://github.com/boykopovar/AnyPS5.git main:refs/review/main-20261029 refs/pull/11893/head:refs/review/pr-11893-20261029 git merge-base refs/review/main-20261029 refs/review/pr-11893-20261029 git merge-tree --write-tree refs/review/main-20261029 refs/review/pr-11893-20261029 base=$(git merge-base refs/review/main-20261029 refs/review/pr-11893-20261029) comm -12 <(git diff --name-only "$base" refs/review/main-20261029 | sort) <(git diff --name-only "$base" refs/review/pr-11893-20261029 | sort) ``` ## Results | Check | Result | |---|---| | Current-main merge preview | Clean | | Touched paths changed on current main since merge base | `docs/dev/debt/libc.md` | | Test argument types | The `%Ld` test passes an `int`; the `%Ln` test writes into guarded `int` storage | | Focused formatting tests | Author reports Linux 3/3 on this exact head; a descendant #11907 run also covers it | | Title-side comparisons | Author reports bundled-title comparisons for the documented formats | | Local build/tests | Not run | <details> <summary>Command output</summary> ```text head=17c950f0a54f8e44ec1e7561034ab6d7086ef824 merge-base=8ff4baf53489033dae79090640143e82d159ee4d merge-preview=52099122bfdefc19afa3ca84bfed047fcf8befdf touched-path-intersection: docs/dev/debt/libc.md ``` </details> ## Negative control | Assertion | Change to code under test | Outcome | |---|---|---| | Corrected `%Ld` and `%Ln` tests validate the actual argument types and write width | The PR changes only the test inputs/guard storage for these cases | The tests now avoid the prior type mismatch and over-sized write; no separate base-source negative control was run here | ## Findings ### Blocking - The branch is based on `8ff4baf534`, not current `main` `3a2b91981651`. The merge preview is clean, but the contribution rules require keeping the branch current. Rebase and rerun the focused tests on that base before approval. ### Non-blocking - The requested test fixes are present. The reported #11907 test run uses this exact head as its parent, so a separate run of the old-base head is not needed; the post-rebase run remains necessary. ## Sources - [Contribution rules for current bases](https://github.com/boykopovar/AnyPS5/blob/3a2b91981651569163768854a1fcdc7193b6667b/CONTRIBUTING.md#L57-L60) - [Tests at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/17c950f0a54f8e44ec1e7561034ab6d7086ef824/core/libs/tests/Formatting.cpp) - [Implementation at the reviewed head](https://github.com/boykopovar/AnyPS5/blob/17c950f0a54f8e44ec1e7561034ab6d7086ef824/core/libs/prx/libc/include/WindowsFormatting.hpp) - [Current debt file](https://github.com/boykopovar/AnyPS5/blob/3a2b91981651569163768854a1fcdc7193b6667b/docs/dev/debt/libc.md) ## Not verified - I did not independently run formatting tests or compare against a console. The Windows-host title comparisons and Linux test results are author/reviewer-provided. - The current hosted Linux product job is skipped; it does not validate the Linux routing. ## To get this to a good state Rebase onto current `main`, preserve the updated debt wording, and rerun `guest_formatting` and `guest_wide_formatting` on the new head. Record the exact head/base; I will re-check and rerun the focused tests available here.
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!11893
No description provided.