From patchwork Tue Mar 28 20:15:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 76248 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2480653vqo; Tue, 28 Mar 2023 13:18:15 -0700 (PDT) X-Google-Smtp-Source: AKy350bnN06bI699lq8xl+f5aF68HgmpIfhdtJZdlrZ1kfhAk7e7rH2C79xQKxfxOcxM/wb11YuU X-Received: by 2002:a17:906:33d2:b0:92c:6fbf:4d with SMTP id w18-20020a17090633d200b0092c6fbf004dmr17043039eja.40.1680034695452; Tue, 28 Mar 2023 13:18:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680034695; cv=none; d=google.com; s=arc-20160816; b=u5qpFCjIAobZ3pI/VjITgAH1UGyVmtR3MKgH+flRlavODfKSnwcGt7ApqWQF2GEffA hyXR0qjBtxgFAit640oEF+PtylbU5y2rwPVJJpeDH2sLxnZUhGoL//LzwkrKCQz0GLv/ K7AUUc2Gj9UyolGgOUm07N+QHJnksMpf36r04B2by0qBXi0l+2ohwaVH+DM3cd9pX9ev Hte49ae80O9EarIhBPt4kZj01KDS0cMSVj5sr3XEistmG1IvHGoAZH9x/s4WsmExoMju BDvi1qvpjZ8cJTMwkAuwQPoQtQ00WqMEKy9JhgvUQR51puNhA5b9VtZhf5d3SHTp1ZfO 4qMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=DRyGQUFnr41Xt9zmq8fVAJ3d2/mJ9U6HV1Qxy7gy6lA=; b=JLupVsoDuU86w+kaKZTFEtbqampuoeeVYo3dcZkm3qtzk7ucbBGARjtb7XJisdaCm6 TTxKMdU44zhGa7ZeXdUQLriC1rsqdAvg896H/sdhi1dIXoveZRHEDN6b/XyMbcORnIND 11GBUiKZ3pjfZSv2s84KQK8IGJREJ8oIBebJScCUrGq9QSXpu6GtiNlg7p0p96Ym7cWe 2IcvbnZujggNkGNcxLQJgonTza1hE3o/tpUW83p2IuJoUQDVnioAx4i2AZYirMDbA7wB pth5a5GEMk2/ZLjWzHhR4gc0TOVW0j5LLkaN7WT4vfzsMZEnPl3Q0BvdhM7LqYqBbUud uUEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=VMBElem3; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id f23-20020a170906049700b0092a9c23d2dasi28739550eja.383.2023.03.28.13.18.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 13:18:15 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=VMBElem3; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A6FAE38845F9 for ; Tue, 28 Mar 2023 20:16:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A6FAE38845F9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680034579; bh=DRyGQUFnr41Xt9zmq8fVAJ3d2/mJ9U6HV1Qxy7gy6lA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=VMBElem3qV4gyLsAuYXhoeHwTS7jJDCXJ+Xe/j1TkrjUiMHi7k/7R4ZPzr2XFFJ2g +iQplS880v01yKBw3uv8II8xrWp3WnfdDqB36on7SFssojrfj/EpRZN6pDk6M/L9Ve tuIUCbNeaKsl1CNb5zK/Qg/xnjPWYGH2sUdXDI5w= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id C28E03850840 for ; Tue, 28 Mar 2023 20:15:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C28E03850840 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-658-USyRtZ5YON-hoC5XWDyx2g-1; Tue, 28 Mar 2023 16:15:02 -0400 X-MC-Unique: USyRtZ5YON-hoC5XWDyx2g-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F0FDA801210; Tue, 28 Mar 2023 20:15:01 +0000 (UTC) Received: from localhost (unknown [10.33.36.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id B65B51402C08; Tue, 28 Mar 2023 20:15:01 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: More fixes for null pointers used with std::char_traits Date: Tue, 28 Mar 2023 21:15:01 +0100 Message-Id: <20230328201501.1202700-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1761644060665882278?= X-GMAIL-MSGID: =?utf-8?q?1761644060665882278?= Tested powerpc64le-linux. Pushed to trunk. -- >8 -- The std::char_traits member functions require that [p,p+n) is a valid range, which is true for p==nullptr iff n==0. But we must not call memcpy, memset etc, in that case, as they require non-null pointers even when n==0. This std::char_traits and std::char_traits explicit specializations are already correct, but the primary template has some bugs. libstdc++-v3/ChangeLog: * include/bits/char_traits.h (char_traits::copy): Return without using memcpy if n==0. (char_traits::assign): Likewise for memset. --- libstdc++-v3/include/bits/char_traits.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/libstdc++-v3/include/bits/char_traits.h b/libstdc++-v3/include/bits/char_traits.h index cfc7d0f0e14..68ed827f982 100644 --- a/libstdc++-v3/include/bits/char_traits.h +++ b/libstdc++-v3/include/bits/char_traits.h @@ -263,6 +263,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION char_traits<_CharT>:: copy(char_type* __s1, const char_type* __s2, std::size_t __n) { + if (__n == 0) + return __s1; #if __cplusplus >= 202002L if (std::__is_constant_evaluated()) { @@ -271,7 +273,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __s1; } #endif - __builtin_memcpy(__s1, __s2, __n * sizeof(char_type)); return __s1; } @@ -293,9 +294,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION if _GLIBCXX17_CONSTEXPR (sizeof(_CharT) == 1 && __is_trivial(_CharT)) { - unsigned char __c; - __builtin_memcpy(&__c, __builtin_addressof(__a), 1); - __builtin_memset(__s, __c, __n); + if (__n) + { + unsigned char __c; + __builtin_memcpy(&__c, __builtin_addressof(__a), 1); + __builtin_memset(__s, __c, __n); + } } else {