Skip to content

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

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
DariusHQ:fix/libc-format-positional-arguments
Oct 11, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
DariusHQ:fix/libc-format-positional-arguments

Conversation

@DariusHQ

Copy link
Copy Markdown
Contributor

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:

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: fix(libc): accept the ' flag in Windows formatting #3567 (both change the same TechnicalDebt line; this branch is on top of it)
  • AI-assisted: yes

FreeBSD's vfprintf takes ' as the thousands-grouping flag. The Windows
formatter threw on it as an unsupported conversion. The guest's setlocale
only accepts the C locale, which has no thousands separator, so the flag
changes nothing: it is dropped before the host's printf sees it, and the
host's locale cannot add separators.
The guest formatter threw on %n$ and *m$, which FreeBSD's vfprintf and
glibc accept. It now reads the arguments a numbered format names into a
table first, with the types their conversions give them, and follows
FreeBSD's rules for unnumbered conversions and unnamed arguments.
@adriadam10

Copy link
Copy Markdown
Collaborator

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.

@adriadam10
adriadam10 merged commit 752db7d into boykopovar:main Oct 11, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants