Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <20261203184300.GK5716@brightrain.aerifal.cx>
Date: Thu, 3 Dec 2026 13:43:00 -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.
> 
>     7d358599d4c8 ("prevent invalid reads of nl_arg in printf_core") made the
>     first pass stop reading nl_arg here, on the premise that it "gets
>     initialized at the end of the first run". That does not hold in this
>     case.

I don't see how this change is relevant to the bug. Either way, values
were never filled into nl_arg[] array if the l10n flag was not set,
and the second pass would thereby read an indeterminate value.

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.