[gcc r16-9434] libstdc++: Disable more false positives in shared_ptr [PR122197]
Jonathan Wakely via Gcc-cvs <[email protected]> Wed, 29 Jul 2026 14:15:33 +0000 (GMT)
| Newsgroups | gmane.comp.gcc.cvs |
|---|---|
| Message-ID | <20260729141533.2F5334BB3BDC__22661.0230094352$1785334541$gmane$org@sourceware.org> |
https://gcc.gnu.org/g:b6274f4ec32dc4a8855459384ac899316e7a34f2 commit r16-9434-gb6274f4ec32dc4a8855459384ac899316e7a34f2 Author: Jonathan Wakely <[email protected]> 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. (cherry picked from commit 34b57b2e14b87f0b1261c0252f404b38f0c1aa1b) 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 b92e3a4c90e4..f53dc48419bc 100644 --- a/libstdc++-v3/include/bits/shared_ptr_base.h +++ b/libstdc++-v3/include/bits/shared_ptr_base.h @@ -581,13 +581,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_impl._M_del()(_M_impl._M_ptr); } +#pragma GCC diagnostic pop virtual void _M_destroy() noexcept @@ -674,13 +675,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_impl._M_alloc(), _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; +}