From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id B4FAB3858289; Tue, 28 Mar 2023 23:35:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B4FAB3858289 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1680046510; bh=Liip5AZljKRUso2gTMJ7fbOiW8ucIpRq1aspMDbNBg4=; h=From:To:Subject:Date:From; b=YAII1pmQOFVN5O9EmPfg5bv0gnyl3fY4sXpjiDBg6tvDCqSUH/h1QI/UR9XQf33rz WR2zeggcqxWjsLd1e3xszNgLnoPr71SOBJto3CzCKIW9vJsLof3P3V6ycrD5nB15qO X+o3wphHPmZwVYjEiQo9rZ8qgzYNKEPVhNh48E5w= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-9345] libstdc++: Add returns_nonnull to non-inline std::map detail [PR108554] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: 33ff7cb3386fb9477f145fa4ab74bdf70d0cb2f3 X-Git-Newrev: 2fdfa3768b25c85df39eaf9b850e130e42a4dd6f Message-Id: <20230328233510.B4FAB3858289@sourceware.org> Date: Tue, 28 Mar 2023 23:35:10 +0000 (GMT) List-Id: https://gcc.gnu.org/g:2fdfa3768b25c85df39eaf9b850e130e42a4dd6f commit r12-9345-g2fdfa3768b25c85df39eaf9b850e130e42a4dd6f Author: Jonathan Wakely Date: Thu Jan 26 10:55:28 2023 +0000 libstdc++: Add returns_nonnull to non-inline std::map detail [PR108554] 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. (cherry picked from commit 3376467ce090aa0966d59ca3aea35db4f17a4b47) Diff: --- libstdc++-v3/include/bits/stl_tree.h | 2 ++ .../testsuite/23_containers/map/modifiers/108554.cc | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/libstdc++-v3/include/bits/stl_tree.h b/libstdc++-v3/include/bits/stl_tree.h index a4de6141765..7a352c17b22 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..811a479b382 --- /dev/null +++ b/libstdc++-v3/testsuite/23_containers/map/modifiers/108554.cc @@ -0,0 +1,21 @@ +// { 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. + +// { dg-bogus "null pointer dereference" "PR 108554" { target *-*-* } 0 } + +#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; +}