|
|
Message-ID: <20261203144857.GI5716@brightrain.aerifal.cx>
Date: Thu, 3 Dec 2026 09:48:57 -0500
From: Rich Felker <dalias@...c.org>
To: Wenkai Xing <wenkaixing12@...il.com>
Cc: musl@...ts.openwall.com
Subject: Re: positional precision (%.*N$) reads uninitialized nl_arg
On Thu, Dec 03, 2026 at 03:31:32PM +0800, Wenkai Xing wrote:
> Hi,
>
> printf_core() runs twice: the first pass (f == 0) records the types of
> positional arguments and, at its end, pops their values into nl_arg[].
> That final loop is only reached when l10n is set.
>
> The width form %*N$ sets l10n, but the precision form %.*N$ does not:
>
> if (isdigit(s[2]) && s[3]=='$') {
> if (!f) nl_type[s[2]-'0'] = INT, p = 0;
> else p = nl_arg[s[2]-'0'].i;
> s+=4;
> }
>
> So when a positional precision is the only positional reference in the
> format, the first pass returns early at "if (!l10n) return 0;" without
> filling nl_arg[], and the second pass reads an uninitialized slot.
>
> Reproducer (1.2.5 and master):
>
> #include <errno.h>
> #include <stdio.h>
>
> int main(void)
> {
> errno = ENOENT;
> printf("[%.*1$m]\n", 3);
> return 0;
> }
>
> Expected "[No ]" (strerror(ENOENT) cut to 3 characters; glibc prints
> that). The precision actually used is whatever is in the uninitialized
> nl_arg[1], so the output is indeterminate: the message is truncated to
> an arbitrary length, possibly to nothing, or printed in full.
>
> %m is the natural trigger because it consumes no argument of its own,
> which lets the precision be the only positional reference.
Yes, %m is the only such trigger because it's the only thing that
doesn't consume an argument.
Rich
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.