From patchwork Tue Jun 13 22:48:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joseph Myers X-Patchwork-Id: 1794695 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QgkHN5xWNz20Vx for ; Wed, 14 Jun 2023 08:48:32 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 94F02385841B for ; Tue, 13 Jun 2023 22:48:30 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from esa3.mentor.iphmx.com (esa3.mentor.iphmx.com [68.232.137.180]) by sourceware.org (Postfix) with ESMTPS id C1B253858D38 for ; Tue, 13 Jun 2023 22:48:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C1B253858D38 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="6.00,241,1681200000"; d="scan'208";a="8700282" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa3.mentor.iphmx.com with ESMTP; 13 Jun 2023 14:48:15 -0800 IronPort-SDR: Nt4b0Y7LuzpmtudRv3ROezwwmKTrg4LSxMjoNO+XfTIG7bGhAI4tDzjfdXdlOgyCn0zwLm+D4Y TrnLbAUdeTC55n06un5xfvYnUjF7zSi48ZiA5k+qoPooY1TJz7tYTi+Vhfbr8YOu4wNfAv79xs ae7jXP3ymGGO4YqB34UD7ssITX5rIvbKL+MXFG8f8I8+cxPoYeezvX8nYSdE36VkPiChqh0NgA PJJI1QS7BqmUQjbd4n7Hirq29TTDMUupUwIYl5MbWiXXKWbHF7PNTqabOIKR3aMP7qbD2nR6Th 3nI= Date: Tue, 13 Jun 2023 22:48:10 +0000 From: Joseph Myers To: Subject: C2x scanf %b support Message-ID: <6b6cb25-9fee-5ddc-a1c3-2b3733a753de@codesourcery.com> MIME-Version: 1.0 X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-11.mgc.mentorg.com (139.181.222.11) To svr-ies-mbx-10.mgc.mentorg.com (139.181.222.10) X-Spam-Status: No, score=-3112.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, KAM_SHORT, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" ISO C2x defines scanf %b for input of binary integers (with an optional 0b or 0B prefix). Implement such support, along with the corresponding SCNb* macros in . Unlike the support for binary integers with 0b or 0B prefix with scanf %i, this is supported in all versions of scanf (independent of the standards mode used for compilation), because there are no backwards compatibility concerns (%b wasn't previously a supported format) the way there were for %i. Tested for x86_64 and x86. Reviewed-by: Adhemerval Zanella diff --git a/NEWS b/NEWS index 23051cf139..84c81160ea 100644 --- a/NEWS +++ b/NEWS @@ -17,9 +17,12 @@ Major new features: wcstoull_l, wcstoimax, wcstoumax, wcstoq, wcstouq. Similarly, the following functions support binary integers prefixed by 0b or 0B as input to the %i format: fscanf, scanf, sscanf, vscanf, vsscanf, - vfscanf, fwscanf, wscanf, swscanf, vfwscanf, vwscanf, vswscanf. + vfscanf, fwscanf, wscanf, swscanf, vfwscanf, vwscanf, vswscanf; those + functions also support the %b format for binary integers, with or + without such a prefix and independent of standards mode. -* PRIb* and PRIB* macros from C2X have been added to . +* PRIb*, PRIB* and SCNb* macros from C2X have been added to + . * A new tunable, glibc.pthread.stack_hugetlb, can be used to disable Transparent Huge Pages (THP) in stack allocation at pthread_create. diff --git a/manual/stdio.texi b/manual/stdio.texi index 3820a24f3e..f54517b4ea 100644 --- a/manual/stdio.texi +++ b/manual/stdio.texi @@ -3527,6 +3527,10 @@ Matches an optionally signed integer in any of the formats that the C language defines for specifying an integer constant. @xref{Numeric Input Conversions}. +@item @samp{%b} +Matches an unsigned integer written in binary radix. This is an ISO +C2X feature. @xref{Numeric Input Conversions}. + @item @samp{%o} Matches an unsigned integer written in octal radix. @xref{Numeric Input Conversions}. @@ -3634,11 +3638,13 @@ For example, any of the strings @samp{10}, @samp{0xa}, or @samp{012} could be read in as integers under the @samp{%i} conversion. Each of these specifies a number with decimal value @code{10}. -The @samp{%o}, @samp{%u}, and @samp{%x} conversions match unsigned -integers in octal, decimal, and hexadecimal radices, respectively. The +The @samp{%b}, @samp{%o}, @samp{%u}, and @samp{%x} conversions match unsigned +integers in binary, octal, decimal, and hexadecimal radices, respectively. The syntax that is recognized is the same as that for the @code{strtoul} function (@pxref{Parsing of Integers}) with the appropriate value -(@code{8}, @code{10}, or @code{16}) for the @var{base} argument. +(@code{2}, @code{8}, @code{10}, or @code{16}) for the @var{base} +argument. The @samp{%b} conversion accepts an optional leading +@samp{0b} or @samp{0B} in all standards modes. The @samp{%X} conversion is identical to the @samp{%x} conversion. They both permit either uppercase or lowercase letters to be used as digits. diff --git a/stdio-common/tst-scanf-binary-main.c b/stdio-common/tst-scanf-binary-main.c index 6b75cb32e5..f9b45178db 100644 --- a/stdio-common/tst-scanf-binary-main.c +++ b/stdio-common/tst-scanf-binary-main.c @@ -16,10 +16,12 @@ License along with the GNU C Library; if not, see . */ +#include #include #include #include +#include #include #include @@ -176,6 +178,195 @@ one_check (const CHAR *s, int expected, char expected_c) TEST_COMPARE (ret_c, expected_c); } +/* GCC does not know the %b format before GCC 12. */ +DIAG_PUSH_NEEDS_COMMENT; +#if !__GNUC_PREREQ (12, 0) +DIAG_IGNORE_NEEDS_COMMENT (11, "-Wformat"); +DIAG_IGNORE_NEEDS_COMMENT (11, "-Wformat-extra-args"); +#endif + +static void +one_check_b (const CHAR *s, int expected, char expected_c) +{ + int ret; + FILE *fp; + unsigned int ret_i; + unsigned long int ret_l; + unsigned long long int ret_ll; + char ret_c; + fp = xfopen (INFILE, "w"); + ret = FNX (fput, s) (s, fp); + TEST_VERIFY_EXIT (0 <= ret); + xfclose (fp); + + ret = FNX (s, scanf) (s, L_("%b %c"), &ret_i, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_i, (unsigned int) expected); + TEST_COMPARE (ret_c, expected_c); + fp = xfopen (INFILE, "r"); + ret = FNX (f, scanf) (fp, L_("%b %c"), &ret_i, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_i, (unsigned int) expected); + TEST_COMPARE (ret_c, expected_c); + xfclose (fp); + fp = xfreopen (INFILE, "r", stdin); + ret = FNX (, scanf) (L_("%b %c"), &ret_i, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_i, (unsigned int) expected); + TEST_COMPARE (ret_c, expected_c); + ret = wrap_vsscanf (s, L_("%b %c"), &ret_i, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_i, (unsigned int) expected); + TEST_COMPARE (ret_c, expected_c); + fp = xfopen (INFILE, "r"); + ret = wrap_vfscanf (fp, L_("%b %c"), &ret_i, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_i, (unsigned int) expected); + TEST_COMPARE (ret_c, expected_c); + xfclose (fp); + fp = xfreopen (INFILE, "r", stdin); + ret = wrap_vscanf (L_("%b %c"), &ret_i, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_i, (unsigned int) expected); + TEST_COMPARE (ret_c, expected_c); + + ret = FNX (s, scanf) (s, L_("%lb %c"), &ret_l, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_l, (unsigned long int) expected); + TEST_COMPARE (ret_c, expected_c); + fp = xfopen (INFILE, "r"); + ret = FNX (f, scanf) (fp, L_("%lb %c"), &ret_l, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_l, (unsigned long int) expected); + TEST_COMPARE (ret_c, expected_c); + xfclose (fp); + fp = xfreopen (INFILE, "r", stdin); + ret = FNX (, scanf) (L_("%lb %c"), &ret_l, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_l, (unsigned long int) expected); + TEST_COMPARE (ret_c, expected_c); + ret = wrap_vsscanf (s, L_("%lb %c"), &ret_l, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_l, (unsigned long int) expected); + TEST_COMPARE (ret_c, expected_c); + fp = xfopen (INFILE, "r"); + ret = wrap_vfscanf (fp, L_("%lb %c"), &ret_l, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_l, (unsigned long int) expected); + TEST_COMPARE (ret_c, expected_c); + xfclose (fp); + fp = xfreopen (INFILE, "r", stdin); + ret = wrap_vscanf (L_("%lb %c"), &ret_l, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_l, (unsigned long int) expected); + TEST_COMPARE (ret_c, expected_c); + + ret = FNX (s, scanf) (s, L_("%llb %c"), &ret_ll, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_ll, (unsigned long long int) expected); + TEST_COMPARE (ret_c, expected_c); + fp = xfopen (INFILE, "r"); + ret = FNX (f, scanf) (fp, L_("%llb %c"), &ret_ll, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_ll, (unsigned long long int) expected); + TEST_COMPARE (ret_c, expected_c); + xfclose (fp); + fp = xfreopen (INFILE, "r", stdin); + ret = FNX (, scanf) (L_("%llb %c"), &ret_ll, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_ll, (unsigned long long int) expected); + TEST_COMPARE (ret_c, expected_c); + ret = wrap_vsscanf (s, L_("%llb %c"), &ret_ll, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_ll, (unsigned long long int) expected); + TEST_COMPARE (ret_c, expected_c); + fp = xfopen (INFILE, "r"); + ret = wrap_vfscanf (fp, L_("%llb %c"), &ret_ll, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_ll, (unsigned long long int) expected); + TEST_COMPARE (ret_c, expected_c); + xfclose (fp); + fp = xfreopen (INFILE, "r", stdin); + ret = wrap_vscanf (L_("%llb %c"), &ret_ll, &ret_c); + TEST_COMPARE (ret, 2); + TEST_COMPARE (ret_ll, (unsigned long long int) expected); + TEST_COMPARE (ret_c, expected_c); +} + +#define CHECK_SCNB(TYPE, MACRO, S, EXPECTED, EXPECTED_C) \ + do \ + { \ + int ret; \ + FILE *fp; \ + TYPE ret_t; \ + char ret_c; \ + fp = xfopen (INFILE, "w"); \ + ret = FNX (fput, s) (S, fp); \ + TEST_VERIFY_EXIT (0 <= ret); \ + xfclose (fp); \ + ret = FNX (s, scanf) (S, L_("%") MACRO " %c", &ret_t, &ret_c); \ + TEST_COMPARE (ret, 2); \ + TEST_COMPARE (ret_t, EXPECTED); \ + TEST_COMPARE (ret_c, EXPECTED_C); \ + fp = xfopen (INFILE, "r"); \ + ret = FNX (f, scanf) (fp, L_("%") MACRO " %c", &ret_t, &ret_c); \ + TEST_COMPARE (ret, 2); \ + TEST_COMPARE (ret_t, EXPECTED); \ + TEST_COMPARE (ret_c, EXPECTED_C); \ + xfclose (fp); \ + fp = xfreopen (INFILE, "r", stdin); \ + ret = FNX (, scanf) (L_("%") MACRO " %c", &ret_t, &ret_c); \ + TEST_COMPARE (ret, 2); \ + TEST_COMPARE (ret_t, EXPECTED); \ + TEST_COMPARE (ret_c, EXPECTED_C); \ + ret = wrap_vsscanf (S, L_("%") MACRO " %c", &ret_t, &ret_c); \ + TEST_COMPARE (ret, 2); \ + TEST_COMPARE (ret_t, EXPECTED); \ + TEST_COMPARE (ret_c, EXPECTED_C); \ + fp = xfopen (INFILE, "r"); \ + ret = wrap_vfscanf (fp, L_("%") MACRO " %c", &ret_t, &ret_c); \ + TEST_COMPARE (ret, 2); \ + TEST_COMPARE (ret_t, EXPECTED); \ + TEST_COMPARE (ret_c, EXPECTED_C); \ + xfclose (fp); \ + fp = xfreopen (INFILE, "r", stdin); \ + ret = wrap_vscanf (L_("%") MACRO " %c", &ret_t, &ret_c); \ + TEST_COMPARE (ret, 2); \ + TEST_COMPARE (ret_t, EXPECTED); \ + TEST_COMPARE (ret_c, EXPECTED_C); \ + } \ + while (0) + +static void +one_check_scnb (const CHAR *s, int expected, char expected_c) +{ +#if TEST_C2X || defined _GNU_SOURCE + CHECK_SCNB (uint8_t, SCNb8, s, (uint8_t) expected, expected_c); + CHECK_SCNB (uint16_t, SCNb16, s, (uint16_t) expected, expected_c); + CHECK_SCNB (uint32_t, SCNb32, s, (uint32_t) expected, expected_c); + CHECK_SCNB (uint64_t, SCNb64, s, (uint64_t) expected, expected_c); + CHECK_SCNB (uint_least8_t, SCNbLEAST8, s, (uint_least8_t) expected, + expected_c); + CHECK_SCNB (uint_least16_t, SCNbLEAST16, s, (uint_least16_t) expected, + expected_c); + CHECK_SCNB (uint_least32_t, SCNbLEAST32, s, (uint_least32_t) expected, + expected_c); + CHECK_SCNB (uint_least64_t, SCNbLEAST64, s, (uint_least64_t) expected, + expected_c); + CHECK_SCNB (uint_fast8_t, SCNbFAST8, s, (uint_fast8_t) expected, expected_c); + CHECK_SCNB (uint_fast16_t, SCNbFAST16, s, (uint_fast16_t) expected, + expected_c); + CHECK_SCNB (uint_fast32_t, SCNbFAST32, s, (uint_fast32_t) expected, + expected_c); + CHECK_SCNB (uint_fast64_t, SCNbFAST64, s, (uint_fast64_t) expected, + expected_c); + CHECK_SCNB (uintmax_t, SCNbMAX, s, (uintmax_t) expected, expected_c); + CHECK_SCNB (uintptr_t, SCNbPTR, s, (uintptr_t) expected, expected_c); +#endif +} + +DIAG_POP_NEEDS_COMMENT; + static int do_test (void) { @@ -183,6 +374,18 @@ do_test (void) one_check (L_("0B101 x"), 5, 'x'); one_check (L_("-0b11111 y"), -31, 'y'); one_check (L_("-0B11111 y"), -31, 'y'); + one_check_b (L_("0b101 x"), 5, 'x'); + one_check_b (L_("0B101 x"), 5, 'x'); + one_check_b (L_("-0b11111 y"), -31, 'y'); + one_check_b (L_("-0B11111 y"), -31, 'y'); + one_check_b (L_("101 x"), 5, 'x'); + one_check_b (L_("-11111 y"), -31, 'y'); + one_check_scnb (L_("0b101 x"), 5, 'x'); + one_check_scnb (L_("0B101 x"), 5, 'x'); + one_check_scnb (L_("-0b11111 y"), -31, 'y'); + one_check_scnb (L_("-0B11111 y"), -31, 'y'); + one_check_scnb (L_("101 x"), 5, 'x'); + one_check_scnb (L_("-11111 y"), -31, 'y'); return 0; } diff --git a/stdio-common/vfscanf-internal.c b/stdio-common/vfscanf-internal.c index a3ebf63554..bfb9baa21a 100644 --- a/stdio-common/vfscanf-internal.c +++ b/stdio-common/vfscanf-internal.c @@ -1381,6 +1381,10 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr, base = 8; goto number; + case L_('b'): /* Binary integer. */ + base = 2; + goto number; + case L_('u'): /* Unsigned decimal integer. */ base = 10; goto number; @@ -1428,10 +1432,11 @@ __vfscanf_internal (FILE *s, const char *format, va_list argptr, c = inchar (); } } - else if ((mode_flags & SCANF_ISOC23_BIN_CST) != 0 - && base == 0 - && width != 0 - && TOLOWER (c) == L_('b')) + else if (width != 0 + && TOLOWER (c) == L_('b') + && (base == 2 + || ((mode_flags & SCANF_ISOC23_BIN_CST) != 0 + && base == 0))) { base = 2; if (width > 0) diff --git a/stdlib/inttypes.h b/stdlib/inttypes.h index 8f0fa8ab9f..42ecf681cc 100644 --- a/stdlib/inttypes.h +++ b/stdlib/inttypes.h @@ -302,6 +302,28 @@ typedef wchar_t __gwchar_t; # define SCNxPTR __PRIPTR_PREFIX "x" +/* Binary notation. */ +# if __GLIBC_USE (ISOC2X) +# define SCNb8 "hhb" +# define SCNb16 "hb" +# define SCNb32 "b" +# define SCNb64 __PRI64_PREFIX "b" + +# define SCNbLEAST8 "hhb" +# define SCNbLEAST16 "hb" +# define SCNbLEAST32 "b" +# define SCNbLEAST64 __PRI64_PREFIX "b" + +# define SCNbFAST8 "hhb" +# define SCNbFAST16 __PRIPTR_PREFIX "b" +# define SCNbFAST32 __PRIPTR_PREFIX "b" +# define SCNbFAST64 __PRI64_PREFIX "b" + +# define SCNbMAX __PRI64_PREFIX "b" +# define SCNbPTR __PRIPTR_PREFIX "b" +# endif + + __BEGIN_DECLS #if __WORDSIZE == 64