On 27/04/20 17:34 -0400, Patrick Palka via Libstdc++ wrote:
This implements the proposed resolution of LWG 3433, which fixes
subrange::advance when called with a negative argument.
Tested on x86_64-pc-linux-gnu, does this look OK to commit?
libstdc++-v3/ChangeLog:
LWG 3433 subrange::advance(n) has UB when n < 0
* include/std/ranges (subrange::prev): Fix typo.
(subrange::advance): Handle a negative argument as per the proposed
resolution of LWG 3433.
* testsuite/std/ranges/subrange/lwg3433.cc: New test.
---
libstdc++-v3/include/std/ranges | 25 +++--
.../testsuite/std/ranges/subrange/lwg3433.cc | 96 +++++++++++++++++++
2 files changed, 111 insertions(+), 10 deletions(-)
create mode 100644 libstdc++-v3/testsuite/std/ranges/subrange/lwg3433.cc
diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 8f91598c26e..565366a8d2f 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -353,23 +353,28 @@ namespace ranges
requires bidirectional_iterator<_It>
{
auto __tmp = *this;
- __tmp.advance(--__n);
+ __tmp.advance(-__n);
return __tmp;
}
constexpr subrange&
advance(iter_difference_t<_It> __n)
{
+ // We incorporate the proposed resolution of LWG 3433 here,
+ // avoiding undefined behavior when __n < 0.
Please replace or extend this comment with our convention for marking
issue resolutions:
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// 3343. subrange::advance(n) has UB when n < 0
OK for trunk with that change, thanks.