Skip to content
Snippets Groups Projects
  • Jonathan Wakely's avatar
    34d8c842
    libstdc++: Use safe integer comparisons in std::latch [PR98749] · 34d8c842
    Jonathan Wakely authored
    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.
    34d8c842
    History
    libstdc++: Use safe integer comparisons in std::latch [PR98749]
    Jonathan Wakely authored
    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.