[Bug 299291] printf(3): numbered F-conversions broken - fetch wrong arguments

From: <bugzilla-noreply_at_freebsd.org>
Date: Sun, 11 Oct 2026 03:55:23 UTC
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=299291

            Bug ID: 299291
           Summary: printf(3): numbered F-conversions broken - fetch wrong
                    arguments
           Product: Base System
           Version: CURRENT
          Hardware: Any
               URL: https://pubs.opengroup.org/onlinepubs/9799919799/funct
                    ions/fprintf.html
                OS: Any
            Status: New
          Severity: Affects Some People
          Priority: ---
         Component: bin
          Assignee: bugs@FreeBSD.org
          Reporter: mi@FreeBSD.org

Created attachment 275574
  --> https://bugs.freebsd.org/bugzilla/attachment.cgi?id=275574&action=edit
Add test for the problem

POSIX defines the F conversion as f except for the case of INF and NAN,
and allows every conversion to be written in the numbered form "%n$":

  https://pubs.opengroup.org/onlinepubs/9799919799/functions/fprintf.html

A numbered F conversion makes printf(3) and wprintf(3) read the double,
and every argument after it, from the wrong place:

    #include <stdio.h>
    #include <wchar.h>

    int
    main(void)
    {
        wchar_t w[64];

        printf("%1$f %2$d\n", 1.5, 7);          /* 1.500000 7 */
        printf("%1$F %2$d\n", 1.5, 7);          /* should be the same */
        swprintf(w, 64, L"%1$F %2$d", 1.5, 7);
        printf("%ls\n", w);                     /* should be the same */
        return (0);
    }

    $ cc -o repro-printf repro-printf.c && ./repro-printf
    1.500000 7
    1.500000 552331777
    0.000000 597837128

%1$e, %1$E, %1$f, %1$g, %1$G, %1$a and %1$A all work.

The __find_arguments() and __find_warguments in
lib/libc/stdio/printf-pos.c, have no case for 'F', so the double is never
recorded. vfprintf.c itself handles 'F' together with 'f' -- and works
correctly. Maybe, the arguments-parsing should be unified for all *printf
functions.

For reference, OpenBSD seems to not have this problem.

-- 
You are receiving this mail because:
You are the assignee for the bug.