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.
---

Tested x86_64-linux. Pushed to trunk. Backport to gcc-16 to follow.

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

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;
+}
-- 
2.55.0

Reply via email to