Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAJoZT2h4DPObxdxCW9dJ6-u0BGG=WBkVmN2iRPunzB1KwHuEmg@mail.gmail.com>
Date: Thu, 3 Dec 2026 15:51:10 +0800
From: Wenkai Xing <wenkaixing12@...il.com>
To: musl@...ts.openwall.com
Subject: wprintf: %p fails with EINVAL

Hi,

    On musl 1.2.4 and 1.2.5 (x86_64), wprintf() rejects %p, while printf()



    handles the same pointer fine.

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

    int main(void)
    {
        int r = wprintf(L"%p", (void *)0x1234);
        fprintf(stderr, "wprintf returned %d, errno = %d\n", r, errno);
        return 0;
    }

    Observed: nothing on stdout, and "wprintf returned -1, errno = 22"



    (EINVAL). glibc prints 0x1234.

    wprintf_core() does not format numbers itself. It builds a narrow
    format string and passes it to fprintf(). The length modifier comes
    from sizeprefix[], which maps 'p' to 'j', so the format built for %p is
    "%*.*jp". But the narrow printf_core() only accepts 'p' directly after
    '%'; after the 'j' modifier its state table has just d, i, o, u, x, X
    and n. So the inner fprintf() rejects the directive with EINVAL. The
    relevant code is unchanged in master.

    Every other conversion yields a combination the narrow core accepts
    (e.g. "%*.*jd", "%*.*jx", "%*.*Lf"); only 'j' with 'p' is invalid.

    wprintf_core() also adds the -1 from the inner fprintf() to its output
    count as if the directive had produced -1 characters, so trailing text
    hides the failure: wprintf(L"p=%p end\n", (void *)0x1234) prints
    "p= end\n" and returns 6.

    Possible fix (not tested): in wprintf_core(), handle 'p' the way the
    narrow core does before building the format string, i.e. force
    ALT_FORM, set p = MAX(p, 2*sizeof(void*)) and convert as 'x'. The
    format passed down is then "%#*.*jx".

    Thanks,
    KevinX

Content of type "text/html" skipped

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.