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: 63736 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3FCBF3858024 for ; Thu, 26 Jan 2023 13:41:02 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3FCBF3858024 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1674740462; bh=xoFAsVWRs111qDHLkHgtbEUg6yWk/QshsyM1LKxQGrA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=LMNOHzqJef5MW6G7qEwVIemMNan5GP4iVgw4iJSae96VU+z+TYHOYOGXCh4+QMy7w yguSPeODbJh1k3OJKm5jEE/ziYAVLXyGTbnD/OQKHFnYQVBP1Zqj5DUkmUlvVBkR3J 4rwJ+JO/ssfQRQNXuE3C+OMYAIjmRu1IubBFz/KY= 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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; +}