fix(libc): format positional arguments on Windows #3569

Merged
DariusHQ merged 2 commits from upstream-pr/3569 into main 2026-10-11 21:40:53 +00:00
DariusHQ commented 2026-10-11 13:27:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DariusHQ on 2026-10-11T13:27:10Z.


What

The guest formatter (FormatWindows) threw Unsupported format conversion on positional arguments (%n$ and *m$), which FreeBSD's vfprintf and glibc accept. Windows runs it for every format, so printf("%2$s %1$d", 7, "x") from a title aborted there while Linux handed the same format to glibc. Linux runs it for formats with a guest wide conversion (%ls, %lc, %S, %C), so a positional format with one of those threw on both.

When a format names a position, the formatter now scans it first, as FreeBSD's __find_arguments does. Each argument up to the highest position gets the type its conversions give it: an integer or pointer, a double, or a long double for L. The formatter reads them from the guest va_list in that order into a table and formats from the table. Where POSIX leaves the result undefined, it follows FreeBSD:

  • a conversion without a position takes the argument after the previous conversion's, so %2$d %d takes 2 then 3;
  • *m$ does not move that position;
  • an argument that no conversion names is read as an int.

POSIX allows positions 1 to NL_ARGMAX, which is 99 on FreeBSD. 0 and anything above 99 throw, so a malformed format cannot make the formatter read an arbitrarily long table off the guest stack. TechnicalDebt lists that limit where it listed positional arguments.

Formats without a position are formatted as before.

Tested

Windows 11 (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build with BUILD_TESTING=ON on main a11c8f7f7 with four branches merged together: this one and the %#.0o, ' flag, positional-argument and sendfile branches opened alongside it: ctest 833/837 passed. The four failures fail the same way on main here:

  • windows_exceptions (#3411);
  • guest_kernel_sys: symlink throws Function not implemented on Windows, which #3510 addresses;
  • guest_filesystem: restoring the directory time fails on Windows, which #3518 addresses;
  • agc_driver_emulated_color_compare: the GPU here cannot sample the sRGB image it uses.

guest_formatting now runs the positional check that was Linux-only (%%ls:%2$d:%1$d) on Windows too, and new checks through sprintf, snprintf and vsnprintf:

  • an argument used twice, and int, hh, double, long double and char* arguments out of order;
  • nine int arguments in reverse, past the registers into the stack area;
  • ten double arguments and an int in reverse;
  • width and precision from positions, a negative width and precision from a position, and a width position after every value position;
  • %ls and %C by position;
  • the three FreeBSD rules above, in formats with %ls so that Linux formats them the same way;
  • positions 0 and 100 throw.

Mutation check, all caught:

  • *m$ moving the position;
  • L read as double;
  • *m$ positions left out of the scan, or not making the format positional;
  • %n$ ignored while formatting;
  • position 0 accepted;
  • no upper limit;
  • long double or double arguments read as integers;
  • the table not used.

Not run on Linux (no Linux host here). The change has no platform-specific part, and the new checks without a wide conversion expect what glibc prints.

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 in TechnicalDebt
  • 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: #3567 (both change the same TechnicalDebt line; this branch is on top of it)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4819396728 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3569). Originally posted by [DariusHQ](https://github.com/DariusHQ) on 2026-10-11T13:27:10Z. --- ### What The guest formatter (`FormatWindows`) threw `Unsupported format conversion` on positional arguments (`%n$` and `*m$`), which FreeBSD's `vfprintf` and glibc accept. Windows runs it for every format, so `printf("%2$s %1$d", 7, "x")` from a title aborted there while Linux handed the same format to glibc. Linux runs it for formats with a guest wide conversion (`%ls`, `%lc`, `%S`, `%C`), so a positional format with one of those threw on both. When a format names a position, the formatter now scans it first, as FreeBSD's `__find_arguments` does. Each argument up to the highest position gets the type its conversions give it: an integer or pointer, a `double`, or a `long double` for `L`. The formatter reads them from the guest `va_list` in that order into a table and formats from the table. Where POSIX leaves the result undefined, it follows FreeBSD: - a conversion without a position takes the argument after the previous conversion's, so `%2$d %d` takes 2 then 3; - `*m$` does not move that position; - an argument that no conversion names is read as an `int`. POSIX allows positions 1 to `NL_ARGMAX`, which is 99 on FreeBSD. 0 and anything above 99 throw, so a malformed format cannot make the formatter read an arbitrarily long table off the guest stack. TechnicalDebt lists that limit where it listed positional arguments. Formats without a position are formatted as before. ### Tested Windows 11 (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, full build with `BUILD_TESTING=ON` on `main` `a11c8f7f7` with four branches merged together: this one and the `%#.0o`, `'` flag, positional-argument and `sendfile` branches opened alongside it: `ctest` 833/837 passed. The four failures fail the same way on `main` here: - `windows_exceptions` (#3411); - `guest_kernel_sys`: `symlink` throws `Function not implemented` on Windows, which #3510 addresses; - `guest_filesystem`: restoring the directory time fails on Windows, which #3518 addresses; - `agc_driver_emulated_color_compare`: the GPU here cannot sample the sRGB image it uses. `guest_formatting` now runs the positional check that was Linux-only (`%%ls:%2$d:%1$d`) on Windows too, and new checks through `sprintf`, `snprintf` and `vsnprintf`: - an argument used twice, and `int`, `hh`, `double`, `long double` and `char*` arguments out of order; - nine `int` arguments in reverse, past the registers into the stack area; - ten `double` arguments and an `int` in reverse; - width and precision from positions, a negative width and precision from a position, and a width position after every value position; - `%ls` and `%C` by position; - the three FreeBSD rules above, in formats with `%ls` so that Linux formats them the same way; - positions 0 and 100 throw. Mutation check, all caught: - `*m$` moving the position; - `L` read as `double`; - `*m$` positions left out of the scan, or not making the format positional; - `%n$` ignored while formatting; - position 0 accepted; - no upper limit; - `long double` or `double` arguments read as integers; - the table not used. Not run on Linux (no Linux host here). The change has no platform-specific part, and the new checks without a wide conversion expect what glibc prints. ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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: #3567 (both change the same TechnicalDebt line; this branch is on top of it) - [x] AI-assisted: yes
adriadam10 commented 2026-10-11 21:40:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:40:49Z.


Checked the positional-argument scan against POSIX/FreeBSD __find_arguments (argument classes, NL_ARGMAX 99, undefined cases following FreeBSD), built and ran guest_formatting and guest_wide_formatting on Linux (the new checks fail without the change) and read the TechnicalDebt update; merging once #3567 is in.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114088338 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3569#issuecomment-6114088338). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:40:49Z. --- Checked the positional-argument scan against POSIX/FreeBSD `__find_arguments` (argument classes, `NL_ARGMAX` 99, undefined cases following FreeBSD), built and ran `guest_formatting` and `guest_wide_formatting` on Linux (the new checks fail without the change) and read the TechnicalDebt update; merging once #3567 is in.
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!3569
No description provided.