From patchwork Thu Mar 16 15:14:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bruno Haible X-Patchwork-Id: 1757926 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=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=clisp.org header.i=@clisp.org header.a=rsa-sha256 header.s=strato-dkim-0002 header.b=QEhyO63q; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (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 4PcrRR11CQz246q for ; Fri, 17 Mar 2023 02:15:15 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 61EC4385B52E for ; Thu, 16 Mar 2023 15:15:11 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mo4-p00-ob.smtp.rzone.de (mo4-p00-ob.smtp.rzone.de [85.215.255.25]) by sourceware.org (Postfix) with ESMTPS id 12BD63858D35 for ; Thu, 16 Mar 2023 15:15:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 12BD63858D35 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=clisp.org Authentication-Results: sourceware.org; spf=none smtp.mailfrom=clisp.org ARC-Seal: i=1; a=rsa-sha256; t=1678979699; cv=none; d=strato.com; s=strato-dkim-0002; b=Q+2nB71+oCG8imHcVc9768bnAa49YzU6CEhurZ2K/TqGUAqAudt9UGCJo/P4LSCgoB Dofbn+uT2HbMP8KwcLs6FcYffPHmqae5VsBTCQPOPBd63FrQS0UDdtvtuvqePfLgoXrp TwaTM1Wg+HEYoc/RrDya+pHbyy8DXHyd8G6i1o3lcwhQ2ZlWAhZvwuY0NtKg6kH+2Ply 3CpYKl+7Y46uMnHxlE1LNhnIP84ygkixoqQ2GFuzB0Qa5scxIrX269KSXyB208E2BdOp DK1TCHkIt6DD9LJs2DO5Ywk+EmLApuxUTwc2ncfX4wRo15Tya8S8rjho43HobPaXXR6h ZlWw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1678979699; s=strato-dkim-0002; d=strato.com; h=Message-ID:Date:Subject:To:From:Cc:Date:From:Subject:Sender; bh=r7VPEMHbGiorQM0wTqZfUNe8lzQMKc1PBdZaDD+BB7w=; b=M5XU/E7bgUKOOv6p5DYIMsixbgAlSo6JNWOSmmbTFRBcKZXedQACmrSJSQ7cDu3eQe MVQAJcgMlv0QnLG+oW0q59VMrBcu6uLvGwixSifVx4MtMLRNlZWSpU+FKgTd7O80RLuB Ygrl6uTgVT9JIbxAxro+e3gca6IXWUtmRABKYz+1LSP+ljGjWYmSA287g3f79ph9DC5Q VqkqiMgv60NP3+llkTJuDNJYl/qboD3a0IuJAnUKXzNRQGGkqouYlqGMKZDrVmeFg0St uZQLyJq72u3wUvO3fO6/6qY9hxWXq0NNFoP8ux/1dKnQIMGQNxhBm3JKujqSVXFun7Ow UkAQ== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo00 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1678979699; s=strato-dkim-0002; d=clisp.org; h=Message-ID:Date:Subject:To:From:Cc:Date:From:Subject:Sender; bh=r7VPEMHbGiorQM0wTqZfUNe8lzQMKc1PBdZaDD+BB7w=; b=QEhyO63qLPoE4WpGrnFo4R1V3wGu7kWI5dPGmZub50cuZifOYZ4FD7dBXcjLjSaJPt bMXFx/X3iOBiDbRmCwyXaYSHtnQhZkEpFJCvlJrt9nvhRjwtBcwpN4v6jum9p8cpnGPa 1Ya7aYeigaIb9FiIJiEu87dqwfxfNsswq8bEuLnl9T0RSSg2WZaVUL32GmifB29/sgOj mGlKzSZqGqIVSn8zfNomQ9WyMTp+LjL9YhnwXXHOBNz6v8R/idM3E7HG62pQ04O4sJip JzYvyOSeaq+7NbBAnwGafnoK3KQxm1Rmwh/Cj08iCTyY6moiB1QIdPzBGKsVXsAyBFoT nVXw== X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH0WWb0LN8XZoH94zq68+3cfpPCjqM01oglKJFcbRkpszTTrIn53Q==" Received: from nimes.localnet by smtp.strato.de (RZmta 49.3.1 AUTH) with ESMTPSA id Z5c498z2GFEw6Id (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Thu, 16 Mar 2023 16:14:58 +0100 (CET) From: Bruno Haible To: Joseph Myers , libc-alpha@sourceware.org Subject: C2x strtol binary constant handling: Fix special case "0b" Date: Thu, 16 Mar 2023 16:14:58 +0100 Message-ID: <3614039.ZfL8zNpBrT@nimes> MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_NONE, TXREP 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" Hi Joseph, While adding C2x support to the strtol, strtoll, strtoul, strtoull functions in Gnulib, I was glad to see that you have already done the support in glibc. But I noticed that the glibc implementation apparently does not handle the input "0b" correctly. The unit test cases for this special case are: { const char input[] = "0b"; char *ptr; long result; errno = 0; result = strtol (input, &ptr, 2); assert (result == 0L); assert (ptr == input + 1); assert (errno == 0); } { const char input[] = "0b"; char *ptr; long result; errno = 0; result = strtol (input, &ptr, 0); assert (result == 0L); assert (ptr == input + 1); assert (errno == 0); } and likewise for strtoll, strtoul, strtoull. Find attached a correction for this. I have tested it in Gnulib (which has very similar source code as glibc for this functionality), not in Glibc directly. Therefore, please review thoroughly. Bruno From c11497ab8117f27680537abccda211c5be560452 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Thu, 16 Mar 2023 15:25:45 +0100 Subject: [PATCH] C2x strtol binary constant handling: Fix special case "0b". This is a follow-up to commit 64924422a99690d147a166b4de3103f3bf3eaf6c by Joseph Myers. The special case of input "0b" was not handled correctly. Test cases: { const char input[] = "0b"; char *ptr; long result; errno = 0; result = strtol (input, &ptr, 2); assert (result == 0L); assert (ptr == input + 1); assert (errno == 0); } { const char input[] = "0b"; char *ptr; long result; errno = 0; result = strtol (input, &ptr, 0); assert (result == 0L); assert (ptr == input + 1); assert (errno == 0); } * stdlib/strtol_l.c (INTERNAL (__strtol_l)): Handle the binary integers also in the 'noconv' part of the code. --- stdlib/strtol_l.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/stdlib/strtol_l.c b/stdlib/strtol_l.c index 3424c3feab..548b46aa52 100644 --- a/stdlib/strtol_l.c +++ b/stdlib/strtol_l.c @@ -526,11 +526,15 @@ INTERNAL (__strtol_l) (const STRING_TYPE *nptr, STRING_TYPE **endptr, noconv: /* We must handle a special case here: the base is 0 or 16 and the first two characters are '0' and 'x', but the rest are no - hexadecimal digits. This is no error case. We return 0 and - ENDPTR points to the `x`. */ + hexadecimal digits. Likewise when the base is 0 or 2 and the + first two characters are '0' and 'b', but the rest are no binary + digits. This is no error case. We return 0 and ENDPTR points to + the 'x' or 'b'. */ if (endptr != NULL) { - if (save - nptr >= 2 && TOUPPER (save[-1]) == L_('X') + if (save - nptr >= 2 + && (TOUPPER (save[-1]) == L_('X') + || (bin_cst && TOUPPER (save[-1]) == L_('B'))) && save[-2] == L_('0')) *endptr = (STRING_TYPE *) &save[-1]; else -- 2.34.1