[gcc r17-2781] libstdc++: Disable more false positives in shared_ptr [PR122197]

Jonathan Wakely redi@gcc.gnu.org
Wed Jul 29 09:19:39 GMT 2026


https://gcc.gnu.org/g:34b57b2e14b87f0b1261c0252f404b38f0c1aa1b

commit r17-2781-g34b57b2e14b87f0b1261c0252f404b38f0c1aa1b
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Jul 28 17:24:00 2026 +0100

    libstdc++: Disable more false positives in shared_ptr [PR122197]
    
    Extend the warning suppression added in r16-7106-g74e0bb3faacfcc to
    cover -Warray-bounds in the destructors and _M_dispose() members.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/122197
            * include/bits/shared_ptr_base.h (~_Sp_counted_deleter): Disable
            -Warray-bounds warnings.
            (_Sp_counted_deleter::_M_dispose): Likewise.
            (_Sp_counted_ptr_inplace::_M_dispose): Likewise.
            * testsuite/20_util/shared_ptr/dest/122197-2.cc: New test.
            * testsuite/20_util/shared_ptr/dest/122197.cc: New test.

Diff:
---
 libstdc++-v3/include/bits/shared_ptr_base.h        |  5 ++--
 .../testsuite/20_util/shared_ptr/dest/122197-2.cc  | 35 ++++++++++++++++++++++
 .../testsuite/20_util/shared_ptr/dest/122197.cc    | 20 +++++++++++++
 3 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h
index 88cac4a06f41..c8342b7904a5 100644
--- a/libstdc++-v3/include/bits/shared_ptr_base.h
+++ b/libstdc++-v3/include/bits/shared_ptr_base.h
@@ -560,13 +560,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 #pragma GCC diagnostic push // PR tree-optimization/122197
 #pragma GCC diagnostic ignored "-Wfree-nonheap-object"
+#pragma GCC diagnostic ignored "-Warray-bounds"
   template<typename> class auto_ptr;
       ~_Sp_counted_deleter() noexcept { }
-#pragma GCC diagnostic pop
 
       virtual void
       _M_dispose() noexcept
       { _M_del._M_obj(_M_ptr); }
+#pragma GCC diagnostic pop
 
       virtual void
       _M_destroy() noexcept
@@ -642,13 +643,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 #pragma GCC diagnostic push // PR tree-optimization/122197
 #pragma GCC diagnostic ignored "-Warray-bounds"
       ~_Sp_counted_ptr_inplace() noexcept { }
-#pragma GCC diagnostic pop
 
       virtual void
       _M_dispose() noexcept
       {
 	allocator_traits<_Alloc>::destroy(_M_alloc._M_obj, _M_ptr());
       }
+#pragma GCC diagnostic pop
 
       // Override because the allocator needs to know the dynamic type
       virtual void
diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/dest/122197-2.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/dest/122197-2.cc
new file mode 100644
index 000000000000..68b35898571d
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/shared_ptr/dest/122197-2.cc
@@ -0,0 +1,35 @@
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-O2 -Warray-bounds -Wfree-nonheap-object -U_GLIBCXX_ASSERTIONS" }
+
+// Bug 122197 predictive devirtualization vs middle-end warnings since r16-4000
+
+// Bug 2507952 - Bogus array bounds warning in shared_ptr_base.h
+// https://bugzilla.redhat.com/show_bug.cgi?id=2507952
+
+#undef _GLIBCXX_ASSERTIONS
+
+#include <memory>
+
+struct value {
+    int a;
+};
+
+value *value_new( void );
+void value_free( value * );
+
+inline std::shared_ptr<value> foo( void )
+{
+    // Commenting out the custom free in this line "fixes" the error
+    return { value_new(), value_free };
+}
+
+struct baz {
+    int x;
+};
+
+extern void bar( void );
+
+void bar( void )
+{
+    auto x = std::shared_ptr<baz>( new baz{ .x = 1 } );
+}
diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/dest/122197.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/dest/122197.cc
new file mode 100644
index 000000000000..40c6b21659e8
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/shared_ptr/dest/122197.cc
@@ -0,0 +1,20 @@
+// { dg-do compile { target c++11 } }
+// { dg-additional-options "-O2 -Warray-bounds -Wfree-nonheap-object" }
+
+// Bug 122197 predictive devirtualization vs middle-end warnings since r16-4000
+
+#undef _GLIBCXX_ASSERTIONS
+
+#include <memory>
+#include <condition_variable>
+
+// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122197#c16
+
+extern std::shared_ptr<int> output_;
+void f();
+
+void f()
+{
+    std::shared_ptr<int> output = std::make_shared<int>();
+    output_ = output;
+}


More information about the Libstdc++-cvs mailing list