]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Trap on std::shared_ptr reference count overflow [PR71945]
authorJonathan Wakely <jwakely@redhat.com>
Wed, 16 Jul 2025 23:21:54 +0000 (00:21 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 11 Sep 2025 13:43:57 +0000 (14:43 +0100)
commit9a2b1acda0cb2c32568b5ca82996eb0544a3a4ef
tree5e36e9dc7f1375ea9ff0d3383c01b1d6af908d14
parentfaae3692f75003f5df226ed776d7386bf848dd00
libstdc++: Trap on std::shared_ptr reference count overflow [PR71945]

This adds checks when incrementing the shared count and weak count and
will trap if they would be be incremented past its maximum. The maximum
value is the value at which incrementing it produces an invalid
use_count(). So that is either the maximum positive value of
_Atomic_word, or for targets where we now allow the counters to wrap
around to negative values, the "maximum" value is -1, because that is
the value at which one more increment overflows the usable range and
resets the counter to zero.

For the weak count the maximum is always -1 as we always allow that
count to use nagative values, so we only tap if it wraps all the way
back to zero.

libstdc++-v3/ChangeLog:

PR libstdc++/71945
* include/bits/shared_ptr_base.h (_Sp_counted_base::_S_chk):
Trap if a reference count cannot be incremented any higher.
(_Sp_counted_base::_M_add_ref_copy): Use _S_chk.
(_Sp_counted_base::_M_add_weak_ref): Likewise.
(_Sp_counted_base<_S_mutex>::_M_add_ref_lock_nothrow): Likewise.
(_Sp_counted_base<_S_atomic>::_M_add_ref_lock_nothrow): Likewise.
(_Sp_counted_base<_S_single>::_M_add_ref_copy): Use _S_chk.

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/bits/shared_ptr_base.h