From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 Received: from second.openwall.net (second.openwall.net [193.110.157.125]) by inbox.vuxu.org (Postfix) with SMTP id 159152AD11 for ; Wed, 28 Aug 2024 01:12:37 +0200 (CEST) Received: (qmail 13754 invoked by uid 550); 27 Aug 2024 23:12:25 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 13704 invoked from network); 27 Aug 2024 23:12:24 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724800337; x=1725405137; darn=lists.openwall.com; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=tpXD6MWzEx9m/w5/qwWJopbmOPwxjy/oa4j5+7vC2JU=; b=LFiAb7Z4bS/VdlxAqfyhEQmOH9PaKhwyOfIG3S/sTdq+vSmyR4jHHBl5Lra/0DmlJ9 6Cr1n64oeUHUlapVRIbS8/wIdmMdRP6dWKwFSZxh3nF6HPpXoAXiBAnGVUIbUWqHXWWC jpdkRBGm045qFIaq/4tzCcdLeyejX/F1fQyHCsHIXqE0MWxaqyHCdQyf4wFD5J1abprD AWS1dgw39ctGf+0bvJgyb6FKaBXMjLYUKbdwxJx+a7pd7SYbali0H6KuaN64MGb6Pw2O rfE3lT4elNWkXRwXPffBJUIDuW/5dSmBmNy4Vo2IA1uaddFd80RS9YY6J9NVpmbTuSUA +7hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724800337; x=1725405137; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tpXD6MWzEx9m/w5/qwWJopbmOPwxjy/oa4j5+7vC2JU=; b=pVUeybGtzh6n70Qn3zFdXk3oAy9Wpbds5zIll+ZmeKiyMt6CN0wWDN+fHuyqeJvbJu iDB2i6AV+eWbxdXDQCoduOi7wjBDkDDVeDP8WDOL2ky8usxWmFwWW3exQkXyRKSklz1A RvNDGqHkRcBfIyh+24v+fqxoaWqas0yMmQq0BZR90Y/BPaNbwuuKrvtcYEQXxEr8Hv+8 PL8urOqjyqZDIgEtaWWXNcE57vuOYzNqvQsvN8cPh7+5/d6x33XrSlzCXBVdp7CuyBPP a5RRY8sExCAmHh2lroipP1e/kIzNuuvRoQAVf3BUPK9FuIYsLQiR8+GT8KrrW5y/ccQd Rsdw== X-Gm-Message-State: AOJu0YwvQ7fHGa7JZFWXs1SB8u7Xu6XBpZ1/pjOK8KQAV1m2Tq2lp8rK tFV8pMkYqv633AT7LDlm3Dng3jmzDr63BsEjoDVbKXcWfA62V58Te/YWDXaH X-Google-Smtp-Source: AGHT+IEjer9/R1wVLxkArzRlDEyK2n94yZABRqg/TM1r3z+2YN1b8ScRgvyb72a2PYiuuQXtgcF3dg== X-Received: by 2002:a05:6402:1d4e:b0:5c0:a70a:5d09 with SMTP id 4fb4d7f45d1cf-5c0a70a60cdmr8542235a12.17.1724800336772; Tue, 27 Aug 2024 16:12:16 -0700 (PDT) From: Gabriel Ravier To: musl@lists.openwall.com Cc: Gabriel Ravier Date: Wed, 28 Aug 2024 01:12:11 +0200 Message-ID: X-Mailer: git-send-email 2.46.0 In-Reply-To: References: <20220908163649.634728-1-gabravier@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [musl] [PATCH v3 1/1] vfprintf: support C23 b and B conversion specifiers These specifiers allow for formatted output of binary integers, and have been added to C23 through N2630. The uppoercase B specifier is not made entirely mandatory by C23, as only lowercase specifiers are reserved for the standard, and thus an implementation could have been using uppercase B for an unrelated extension, but C23 still has a note stating it is recommended practice to implement it as the uppercase counterpart of the b specifier. I have tested this on: - glibc's tests for %b and %B - The libc testsuite I'm developing over at https://github.com/GabrielRavier/yalibct - musl's libc-test - musl's libc-testsuite And observed no regressions. --- src/stdio/vfprintf.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c index 360d723a..ec51aa3c 100644 --- a/src/stdio/vfprintf.c +++ b/src/stdio/vfprintf.c @@ -49,7 +49,7 @@ enum { static const unsigned char states[]['z'-'A'+1] = { { /* 0: bare types */ S('d') = INT, S('i') = INT, - S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT, + S('o') = UINT, S('u') = UINT, S('x') = UINT, S('X') = UINT, S('b') = UINT, S('B') = UINT, S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, S('c') = INT, S('C') = UINT, @@ -59,7 +59,7 @@ static const unsigned char states[]['z'-'A'+1] = { S('z') = ZTPRE, S('j') = JPRE, S('t') = ZTPRE, }, { /* 1: l-prefixed */ S('d') = LONG, S('i') = LONG, - S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG, + S('o') = ULONG, S('u') = ULONG, S('x') = ULONG, S('X') = ULONG, S('b') = ULONG, S('B') = ULONG, S('e') = DBL, S('f') = DBL, S('g') = DBL, S('a') = DBL, S('E') = DBL, S('F') = DBL, S('G') = DBL, S('A') = DBL, S('c') = UINT, S('s') = PTR, S('n') = PTR, @@ -68,17 +68,20 @@ static const unsigned char states[]['z'-'A'+1] = { S('d') = LLONG, S('i') = LLONG, S('o') = ULLONG, S('u') = ULLONG, S('x') = ULLONG, S('X') = ULLONG, + S('b') = ULLONG, S('B') = ULLONG, S('n') = PTR, }, { /* 3: h-prefixed */ S('d') = SHORT, S('i') = SHORT, S('o') = USHORT, S('u') = USHORT, S('x') = USHORT, S('X') = USHORT, + S('b') = USHORT, S('B') = USHORT, S('n') = PTR, S('h') = HHPRE, }, { /* 4: hh-prefixed */ S('d') = CHAR, S('i') = CHAR, S('o') = UCHAR, S('u') = UCHAR, S('x') = UCHAR, S('X') = UCHAR, + S('b') = UCHAR, S('B') = UCHAR, S('n') = PTR, }, { /* 5: L-prefixed */ S('e') = LDBL, S('f') = LDBL, S('g') = LDBL, S('a') = LDBL, @@ -88,11 +91,13 @@ static const unsigned char states[]['z'-'A'+1] = { S('d') = PDIFF, S('i') = PDIFF, S('o') = SIZET, S('u') = SIZET, S('x') = SIZET, S('X') = SIZET, + S('b') = SIZET, S('B') = SIZET, S('n') = PTR, }, { /* 7: j-prefixed */ S('d') = IMAX, S('i') = IMAX, S('o') = UMAX, S('u') = UMAX, S('x') = UMAX, S('X') = UMAX, + S('b') = UMAX, S('B') = UMAX, S('n') = PTR, } }; @@ -150,6 +155,12 @@ static const char xdigits[16] = { "0123456789ABCDEF" }; +static char *fmt_b(uintmax_t x, char *s) +{ + for (; x; x>>=1) *--s = '0' + (x&1); + return s; +} + static char *fmt_x(uintmax_t x, char *s, int lower) { for (; x; x>>=4) *--s = xdigits[(x&15)]|lower; @@ -431,7 +442,12 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, unsigned st, ps; int cnt=0, l=0; size_t i; - char buf[sizeof(uintmax_t)*3]; + /* This buffer is used for integer conversions. As such, it needs + * to be able to contain the full representation of a number (without a + * prefix/padding or null terminator) in base 2, 8, 10 or 16, with base + * 2 having the largest possible requirement of as many characters as + * the amount of bits in the largest possible integer type */ + char buf[sizeof(uintmax_t)*CHAR_BIT]; const char *prefix; int t, pl; wchar_t wc[2], *ws; @@ -528,7 +544,7 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, if (ferror(f)) return -1; z = buf + sizeof(buf); - prefix = "-+ 0X0x"; + prefix = "-+ 0X0x0B0b"; pl = 0; t = s[-1]; @@ -558,6 +574,10 @@ static int printf_core(FILE *f, const char *fmt, va_list *ap, union arg *nl_arg, a = fmt_x(arg.i, z, t&32); if (arg.i && (fl & ALT_FORM)) prefix+=(t>>4), pl=2; if (0) { + case 'b': case 'B': + a = fmt_b(arg.i, z); + if (arg.i && (fl & ALT_FORM)) prefix+=9+((t=='b')<<1), pl=2; + } if (0) { case 'o': a = fmt_o(arg.i, z); if ((fl&ALT_FORM) && p