gcc/libstdc++-v3/testsuite/24_iterators/range_operations
Jonathan Wakely f21f22d1ba libstdc++: Avoid overflow in ranges::advance(i, n, bound)
When (bound - i) or n is the most negative value of its type, the
negative of the value will overflow. Instead of abs(n) >= abs(bound - i)
use n >= (bound - i) when positive and n <= (bound - i) when negative.
The function has a precondition that they must have the same sign, so
this works correctly. The precondition check can be moved into the else
branch, and simplified.

The standard requires calling ranges::advance(i, bound) even if i==bound
is already true, which is technically observable, but that's pointless.
We can just return n in that case. Similarly, for i!=bound but n==0 we
are supposed to call ranges::advance(i, n), but that's pointless. An LWG
issue to allow omitting the pointless calls is expected to be filed.

libstdc++-v3/ChangeLog:

	* include/bits/ranges_base.h (ranges::advance): Avoid signed
	overflow. Do nothing if already equal to desired result.
	* testsuite/24_iterators/range_operations/advance_overflow.cc:
	New test.
2022-01-27 22:24:29 +00:00
..
100768.cc Update copyright years. 2022-01-03 10:42:10 +01:00
advance_debug_neg.cc Update copyright years. 2022-01-03 10:42:10 +01:00
advance_overflow.cc libstdc++: Avoid overflow in ranges::advance(i, n, bound) 2022-01-27 22:24:29 +00:00
advance.cc Update copyright years. 2022-01-03 10:42:10 +01:00
distance.cc Update copyright years. 2022-01-03 10:42:10 +01:00
lwg3392.cc libstdc++: Implement LWG 3392 for std::ranges::distance 2021-10-01 20:36:54 +01:00
next.cc Update copyright years. 2022-01-03 10:42:10 +01:00
prev.cc Update copyright years. 2022-01-03 10:42:10 +01:00