https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98749
--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jonathan Wakely <r...@gcc.gnu.org>: https://gcc.gnu.org/g:34d8c842b8d12c69b9335f1b63453920f056e6e1 commit r15-7286-g34d8c842b8d12c69b9335f1b63453920f056e6e1 Author: Jonathan Wakely <jwak...@redhat.com> Date: Thu Jan 9 21:50:31 2025 +0000 libstdc++: Use safe integer comparisons in std::latch [PR98749] The std::latch::max() function assumes that the returned value can be represented by ptrdiff_t, which is true when __platform_wait_t is int (e.g. on Linux) but not when it's unsigned long, which is the case for most other 64-bit targets. We should use the smaller of PTRDIFF_MAX and std::numeric_limits<__platform_wait_t>::max(). Use std::cmp_less to do a safe comparison that works for all types. We can also use std::cmp_less and std::cmp_equal in std::latch::count_down so that we don't need to deal with comparisons between signed and unsigned. Also add a missing precondition check to constructor and fix the existing check in count_down which was duplicated by mistake. libstdc++-v3/ChangeLog: PR libstdc++/98749 * include/std/latch (latch::max()): Ensure the return value is representable as the return type. (latch::latch(ptrdiff_t)): Add assertion. (latch::count_down): Fix copy & pasted duplicate assertion. Use std::cmp_equal to compare __platform_wait_t and ptrdiff_t values. (latch::_M_a): Use defined constant for alignment. * testsuite/30_threads/latch/1.cc: Check max(). Check constant initialization works for values in the valid range. Check alignment.