From patchwork Thu Jan 26 13:39:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 48651 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp274846wrn; Thu, 26 Jan 2023 05:41:19 -0800 (PST) X-Google-Smtp-Source: AMrXdXuxUWqhjUpT6U6AHHckdRuXGEZKTjaFBjScBjGKEwyTre7ZztHJWp1CxLfHi2zCFYlu1vxh X-Received: by 2002:a17:906:8d06:b0:7b5:911c:9b12 with SMTP id rv6-20020a1709068d0600b007b5911c9b12mr36498667ejc.1.1674740479395; Thu, 26 Jan 2023 05:41:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674740479; cv=none; d=google.com; s=arc-20160816; b=NHICnXouYS/o+EjGv2fTSVyFdVHHBTN4J8yblU8k2TGQqtt8iAGyfOAC+ZxXodwiOx 2IxThhbwE0Ap4UcJ96POq9KZuoFhJ1COFNUXaqP5IIdbv99v2l1xsSd00GRPHzeNljol dX4ZluNLwZejZhpl+YUwxDYoY54Vi38d1BIwFHi1eMBBwPnQsWCZvkBl+VT6BPdWHa0u RSYte+C7rWcKMtKfbGXKVfHdANjCEsCgVj/yND8MPy4RGCu9FUJDpZxgn++UXQI2uzqR u7blmZGc9V4t12/HEeB9ji/MPOBY5m/jIWH4Z5u0P14VfKYbae+OiPSIpE995xgEDw50 tquw== 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=xoFAsVWRs111qDHLkHgtbEUg6yWk/QshsyM1LKxQGrA=; b=H/PG4cLUP2i5AutlY191eilUrE3OQyFZDAfp2irN30PnPuYd4Fzqn86+EkR/NMrcNq 0wCu0VhK7wFjUrPr3aX1U434VH40vmGytOng/qkrkBwW9DnBeCb3HRa9tdxEou+KFAWa +CBgbVRY/FL0E2tgdnt4E3Sk0y3rL5FEY4E8BJ0F9jD5L/bVNfy/VTJvrnq5TC8/Y7IY zNlxn2nVa1kcJfnwIlOcIDIVjCy1cu+7mbq5VSuTTMne0e3DYMLurwjV8lmVHugjTe9E ZeRwBcYPievaR8DLpZljWLBjPVa1zSu46xyTf2fKB8SWOR4dAlH+3bmc65n6QGamymIs jHEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=g8afkFZD; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id c29-20020a17090620dd00b008785ec37df7si1800082ejc.249.2023.01.26.05.41.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 26 Jan 2023 05:41:19 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=g8afkFZD; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 C98D63858C2C for ; Thu, 26 Jan 2023 13:41:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C98D63858C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674740476; bh=xoFAsVWRs111qDHLkHgtbEUg6yWk/QshsyM1LKxQGrA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=g8afkFZDclnKJ02qJS7yhvwwV/JCJPXBuuhhIDxQq9S8fToB3ydNxREQmJmZJHhfC 0X/4HGtlhYJiihTf9M0YEFFON28O8U+pQGtOHcCvrFTCkIipWw79bGl6f+VXLTlmbo gFMQywXn2msruACg2OnAtPSVu9MJH4JsR/5oKnTA= 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 9DF50385840A for ; Thu, 26 Jan 2023 13:39:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9DF50385840A 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-182-y9b4NOTjMvmE-L7oKI6wOA-1; Thu, 26 Jan 2023 08:39:51 -0500 X-MC-Unique: y9b4NOTjMvmE-L7oKI6wOA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id B19D6183B3C0; Thu, 26 Jan 2023 13:39:50 +0000 (UTC) Received: from localhost (unknown [10.33.36.186]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7595D40C2064; Thu, 26 Jan 2023 13:39:50 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add returns_nonnull to non-inline std::map detail [PR108554] Date: Thu, 26 Jan 2023 13:39:49 +0000 Message-Id: <20230126133949.1428954-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 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, URI_HEX 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: 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?1756092672919314332?= X-GMAIL-MSGID: =?utf-8?q?1756092672919314332?= Tested x86_64-linux. Pushed to trunk. -- >8 -- std::map uses a non-inline function to rebalance its tree and the compiler can't see that it always returns a valid pointer (assuming valid inputs, which is a precondition anyway). This can result in -Wnull-derefernce warnings for valid code, because the compiler thinks there is a path where the function returns null. Adding the returns_nonnull attribute tells the compiler that is can't happen. While we're doing that, we might as well also add a nonnull attribute to the rebalancing functions too. libstdc++-v3/ChangeLog: PR libstdc++/108554 * include/bits/stl_tree.h (_Rb_tree_insert_and_rebalance): Add nonnull attribute. (_Rb_tree_rebalance_for_erase): Add nonnull and returns_nonnull attributes. * testsuite/23_containers/map/modifiers/108554.cc: New test. --- libstdc++-v3/include/bits/stl_tree.h | 2 ++ .../23_containers/map/modifiers/108554.cc | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 libstdc++-v3/testsuite/23_containers/map/modifiers/108554.cc diff --git a/libstdc++-v3/include/bits/stl_tree.h b/libstdc++-v3/include/bits/stl_tree.h index 5b7a5869b14..3c331fbc952 100644 --- a/libstdc++-v3/include/bits/stl_tree.h +++ b/libstdc++-v3/include/bits/stl_tree.h @@ -405,12 +405,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION _Base_ptr _M_node; }; + __attribute__((__nonnull__)) void _Rb_tree_insert_and_rebalance(const bool __insert_left, _Rb_tree_node_base* __x, _Rb_tree_node_base* __p, _Rb_tree_node_base& __header) throw (); + __attribute__((__nonnull__,__returns_nonnull__)) _Rb_tree_node_base* _Rb_tree_rebalance_for_erase(_Rb_tree_node_base* const __z, _Rb_tree_node_base& __header) throw (); diff --git a/libstdc++-v3/testsuite/23_containers/map/modifiers/108554.cc b/libstdc++-v3/testsuite/23_containers/map/modifiers/108554.cc new file mode 100644 index 00000000000..7076682f4c9 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/map/modifiers/108554.cc @@ -0,0 +1,19 @@ +// { dg-do compile { target c++17 } } +// { dg-options "-Wnull-dereference -O2" } + +// PR libstdc++/108554 +// Warning from -Wnull-dereference when extracting a unique_ptr from a map. + +#include +#include +#include + +int pop(std::map>& m) +{ + if (auto it = m.find("key"); it != m.end()) + { + auto item = std::move(m.extract(it).mapped()); + return *item; + } + return 0; +}