diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2022-01-26 16:08:51 +0000 |
---|---|---|
committer | Jonathan Wakely <jwakely@redhat.com> | 2022-01-27 22:24:29 +0000 |
commit | f21f22d1baf7e90f3edbfc48040c76fb14103803 (patch) | |
tree | 73188c6f671ab823be094485ebad7a562b5caa29 /gcc | |
parent | 66b86171188dcb61d2d0e0a4a98a7467e58a84a7 (diff) | |
download | gcc-f21f22d1baf7e90f3edbfc48040c76fb14103803.zip gcc-f21f22d1baf7e90f3edbfc48040c76fb14103803.tar.gz gcc-f21f22d1baf7e90f3edbfc48040c76fb14103803.tar.bz2 |
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.
Diffstat (limited to 'gcc')
0 files changed, 0 insertions, 0 deletions