]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Fix std::counting_semaphore<> default max value
authorJonathan Wakely <jwakely@redhat.com>
Wed, 26 Nov 2025 14:44:03 +0000 (14:44 +0000)
committerJonathan Wakely <redi@gcc.gnu.org>
Thu, 27 Nov 2025 09:43:24 +0000 (09:43 +0000)
commit89552346e3cef8ef32e2ebe643bb96d085447e68
tree5ece7715de640af6d90577d96fa2772d51892452
parent319a956cd25ccc05c9447d55d76f0c98e8f6b598
libstdc++: Fix std::counting_semaphore<> default max value

My recent (uncommitted) changes to support a 64-bit __platform_wait_t
for FreeBSD and Darwin revealed a problem in std::counting_semaphore.
When the default template argument is used and __platform_wait_t is a
64-bit type, the numeric_limits<__platform_wait_t>::max() value doesn't
fit in ptrdiff_t and so we get ptrdiff_t(-1), which fails a
static_assert in the class body.

The solution is to cap the value to PTRDIFF_MAX instead of allowing it
to go negative.

libstdc++-v3/ChangeLog:

* include/bits/semaphore_base.h (__platform_semaphore::_S_max):
Limit to PTRDIFF_MAX to avoid negative values.
* testsuite/30_threads/semaphore/least_max_value.cc: New test.

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/bits/semaphore_base.h
libstdc++-v3/testsuite/30_threads/semaphore/least_max_value.cc [new file with mode: 0644]