Skip to content

fix(libc): format positional arguments in the wide printf family - #4357

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/wide-printf-positional
Oct 12, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
milescoviello:fix/wide-printf-positional

Conversation

@milescoviello

@milescoviello milescoviello commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What

The wide formatter behind swprintf, vswprintf, wprintf and snwprintf_s does not parse positional arguments (%n$ and *m$). It reads the position digits as a field width, takes $ for the conversion and fails, so on main swprintf(buffer, 32, L"%2$d-%1$d", 1, 2) returns -1 with errno EINVAL and an empty buffer, and L"%2$ls %1$ls" does the same. Localized format strings that reorder their arguments print nothing.

FreeBSD formats wide output with __vfwprintf (vswprintf.c line 79), which takes positions as the narrow vfprintf does. FreeBSD 11.4 lib/libc/stdio/vfwprintf.c lines 635-652:

case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
	n = 0;
	do {
		n = 10 * n + to_digit(ch);
		ch = *fmt++;
	} while (is_digit(ch));
	if (ch == '$') {
		nextarg = n;
		if (argtable == NULL) {
			argtable = statargtable;
			if (__find_warguments (fmt0, orgap,
					       &argtable)) {

and GETASTER reads a *m$ width or precision from position m without moving nextarg.

The wide formatter now numbers its arguments with the narrow formatter's NumberedArgumentClasses and ArgumentPosition (the ones FormatWindows uses since #3569), run over an ASCII copy of the format. The same rules apply: a position selects the argument, *m$ does not move it, and a conversion without a position takes the argument after the previous one. Formats without a position are formatted as before. No title is claimed.

Tested

Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags.

  • guest_wide_formatting gets CheckPositional: swprintf with %2$d-%1$d, %2$ls %1$ls, [%1$*2$d|%3$.*2$f] (width and precision from a position), %1$Lg %2$d %d (a long double by position and a following conversion without one), and snwprintf_s with %2$s%1$lc.
  • With only the test applied to main's library sources it aborts at the first check: snwprintf_s: swprintf positional integers (main returns -1 for every one of the five formats).
  • With the fix it passes, as do guest_formatting and guest_wide_strings, and all 294 guest_* tests pass (-E '^guest_video_out_').

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: none
  • Unblocks: none
  • AI-assisted: yes

The wide formatter behind swprintf, vswprintf, wprintf and snwprintf_s
does not parse %n$ or *m$. It reads the position digits as a field
width, takes '$' for the conversion and fails, so on main
swprintf(buffer, 32, L"%2$d-%1$d", 1, 2) returns -1 with errno EINVAL
and an empty buffer. Localized format strings that reorder their
arguments this way print nothing.

FreeBSD formats wide output with __vfwprintf (vswprintf.c line 79),
which takes positions exactly as the narrow vfprintf does. FreeBSD 11.4
lib/libc/stdio/vfwprintf.c lines 635-652:

	case '1': case '2': case '3': case '4':
	case '5': case '6': case '7': case '8': case '9':
		n = 0;
		do {
			n = 10 * n + to_digit(ch);
			ch = *fmt++;
		} while (is_digit(ch));
		if (ch == '$') {
			nextarg = n;
			if (argtable == NULL) {
				argtable = statargtable;
				if (__find_warguments (fmt0, orgap,
						       &argtable)) {

and GETASTER (lines 509-531) reads a *m$ width or precision from
position m without moving nextarg.

The wide formatter now numbers its arguments with the narrow
formatter's NumberedArgumentClasses and ArgumentPosition, run over an
ASCII copy of the format, so the same FreeBSD rules apply: a position
selects the argument, *m$ does not move it, and a conversion without a
position takes the argument after the previous one. Formats without a
position are formatted as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the libc label Oct 12, 2026
@xXPBSXx

xXPBSXx commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Windows test: head 7ad073603 merged onto current main 33ae282e4 (clean merge, no conflicts). Windows 10, MinGW GCC (WinLibs), Ninja Release, -j4: full build 0 errors; ctest with the filter guest_wide_formatting: guest_wide_formatting passed (1 test(s), 0 failed); python tools/check_conventions.py --base upstream/main passed. I did not run the new tests against main without the change.

AI-assisted (Claude Code).

@oneandonlydean oneandonlydean self-assigned this Oct 12, 2026
@oneandonlydean
oneandonlydean merged commit 2c579b5 into boykopovar:main Oct 12, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants