public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
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	[thread overview]
Message-ID: <20230126133949.1428954-1-jwakely@redhat.com> (raw)

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 <map>
+#include <memory>
+#include <string>
+
+int pop(std::map<std::string, std::unique_ptr<int>>& m)
+{
+  if (auto it = m.find("key"); it != m.end())
+  {
+    auto item = std::move(m.extract(it).mapped());
+    return *item;
+  }
+  return 0;
+}
-- 
2.39.1


                 reply	other threads:[~2023-01-26 13:39 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230126133949.1428954-1-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).