gcc/libstdc++-v3/testsuite/24_iterators
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
..
associated_types Update copyright years. 2022-01-03 10:42:10 +01:00
back_insert_iterator libstdc++: Add attribute to features deprecated in C++17 [PR91260] 2022-01-14 11:27:50 +00:00
bidirectional Update copyright years. 2022-01-03 10:42:10 +01:00
common_iterator libstdc++: Use std::construct_at in std::common_iterator [PR103992] 2022-01-14 10:14:24 +00:00
contiguous Update copyright years. 2022-01-03 10:42:10 +01:00
counted_iterator Update copyright years. 2022-01-03 10:42:10 +01:00
customization_points Update copyright years. 2022-01-03 10:42:10 +01:00
forward Update copyright years. 2022-01-03 10:42:10 +01:00
front_insert_iterator libstdc++: Add attribute to features deprecated in C++17 [PR91260] 2022-01-14 11:27:50 +00:00
headers/iterator Update copyright years. 2022-01-03 10:42:10 +01:00
indirect_callable Update copyright years. 2022-01-03 10:42:10 +01:00
input Update copyright years. 2022-01-03 10:42:10 +01:00
insert_iterator libstdc++: Add attribute to features deprecated in C++17 [PR91260] 2022-01-14 11:27:50 +00:00
istream_iterator libstdc++: Add attribute to features deprecated in C++17 [PR91260] 2022-01-14 11:27:50 +00:00
istreambuf_iterator libstdc++: Add attribute to features deprecated in C++17 [PR91260] 2022-01-14 11:27:50 +00:00
move_iterator Update copyright years. 2022-01-03 10:42:10 +01:00
normal_iterator Update copyright years. 2022-01-03 10:42:10 +01:00
operations Update copyright years. 2022-01-03 10:42:10 +01:00
ostream_iterator libstdc++: Add attribute to features deprecated in C++17 [PR91260] 2022-01-14 11:27:50 +00:00
ostreambuf_iterator libstdc++: Add attribute to features deprecated in C++17 [PR91260] 2022-01-14 11:27:50 +00:00
output Update copyright years. 2022-01-03 10:42:10 +01:00
random_access Update copyright years. 2022-01-03 10:42:10 +01:00
range_access Update copyright years. 2022-01-03 10:42:10 +01:00
range_operations libstdc++: Avoid overflow in ranges::advance(i, n, bound) 2022-01-27 22:24:29 +00:00
reverse_iterator libstdc++: Add attribute to features deprecated in C++17 [PR91260] 2022-01-14 11:27:50 +00:00
aliases.cc Update copyright years. 2022-01-03 10:42:10 +01:00
container_access.cc Update copyright years. 2022-01-03 10:42:10 +01:00