Repository navigation
fix(libc): format positional arguments in the wide printf family - #4357
Merged
oneandonlydean merged 1 commit intoOct 12, 2026
Merged
Conversation
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>
Contributor
|
Windows test: head AI-assisted (Claude Code). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
The wide formatter behind
swprintf,vswprintf,wprintfandsnwprintf_sdoes not parse positional arguments (%n$and*m$). It reads the position digits as a field width, takes$for the conversion and fails, so on mainswprintf(buffer, 32, L"%2$d-%1$d", 1, 2)returns -1 with errnoEINVALand an empty buffer, andL"%2$ls %1$ls"does the same. Localized format strings that reorder their arguments print nothing.FreeBSD formats wide output with
__vfwprintf(vswprintf.cline 79), which takes positions as the narrowvfprintfdoes. FreeBSD 11.4lib/libc/stdio/vfwprintf.clines 635-652:and
GETASTERreads a*m$width or precision from positionmwithout movingnextarg.The wide formatter now numbers its arguments with the narrow formatter's
NumberedArgumentClassesandArgumentPosition(the onesFormatWindowsuses 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_formattinggetsCheckPositional:swprintfwith%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(along doubleby position and a following conversion without one), andsnwprintf_swith%2$s%1$lc.snwprintf_s: swprintf positional integers(main returns -1 for every one of the five formats).guest_formattingandguest_wide_strings, and all 294guest_*tests pass (-E '^guest_video_out_').Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)