Skip to content
Snippets Groups Projects
Unverified Commit 34d8c842 authored by Jonathan Wakely's avatar Jonathan Wakely Committed by Jonathan Wakely
Browse files

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.
parent bea86e82
No related merge requests found
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment