f21f22d1ba
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. |
||
---|---|---|
.. | ||
associated_types | ||
back_insert_iterator | ||
bidirectional | ||
common_iterator | ||
contiguous | ||
counted_iterator | ||
customization_points | ||
forward | ||
front_insert_iterator | ||
headers/iterator | ||
indirect_callable | ||
input | ||
insert_iterator | ||
istream_iterator | ||
istreambuf_iterator | ||
move_iterator | ||
normal_iterator | ||
operations | ||
ostream_iterator | ||
ostreambuf_iterator | ||
output | ||
random_access | ||
range_access | ||
range_operations | ||
reverse_iterator | ||
aliases.cc | ||
container_access.cc |