From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2136) id 9EAFF3948829; Wed, 17 Jun 2020 20:50:43 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9EAFF3948829 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1592427043; bh=12ZV2FK3JxG4ka6l5oQW/IGspYis1/3++8nJ7jyAV4w=; h=From:To:Subject:Date:From; b=gmMhUIJpS+k36DNFpDUX9YUUcx+ingDbna66EQAm6K4Fn0aqSsAo30j8ZwyADJ9iK ozLS4dcDDNLJKIrRfQnUS4v9GTRrfNB22/7TFL2rxkOmdtzVD0vvbBsCQbHGl5Grk3 QUVP/G3BYwPSTg5r9D/NAyb97ZMx5SJ0rZZhAePY= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Aldy Hernandez To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc/devel/ranger] libstdc++: Fix subrange::advance and subrange::prev (LWG 3433) X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/devel/ranger X-Git-Oldrev: 69bc8815740de46869b45403b3b583e1e2c0e442 X-Git-Newrev: 19667c82e479dc2bf8351588ed57aff90220b748 Message-Id: <20200617205043.9EAFF3948829@sourceware.org> Date: Wed, 17 Jun 2020 20:50:43 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Jun 2020 20:50:43 -0000 https://gcc.gnu.org/g:19667c82e479dc2bf8351588ed57aff90220b748 commit 19667c82e479dc2bf8351588ed57aff90220b748 Author: Patrick Palka Date: Tue Apr 28 16:34:24 2020 -0400 libstdc++: Fix subrange::advance and subrange::prev (LWG 3433) This implements the proposed resolution of LWG 3433, which fixes subrange::advance when called with a negative argument. 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. Diff: --- libstdc++-v3/ChangeLog | 8 ++ libstdc++-v3/include/std/ranges | 25 +++--- .../testsuite/std/ranges/subrange/lwg3433.cc | 96 ++++++++++++++++++++++ 3 files changed, 119 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index f6de19da5de..f2ddc5e4776 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2020-04-28 Patrick Palka + + 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. + 2020-04-28 Jonathan Wakely Iain Sandoe diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges index 8f91598c26e..0c602c7200f 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) { + // _GLIBCXX_RESOLVE_LIB_DEFECTS + // 3433. subrange::advance(n) has UB when n < 0 + if constexpr (bidirectional_iterator<_It>) + if (__n < 0) + { + ranges::advance(_M_begin, __n); + if constexpr (_S_store_size) + _M_size._M_size += __detail::__to_unsigned_like(-__n); + return *this; + } + + __glibcxx_assert(__n >= 0); + auto __d = __n - ranges::advance(_M_begin, __n, _M_end); if constexpr (_S_store_size) - { - auto __d = __n - ranges::advance(_M_begin, __n, _M_end); - if (__d >= 0) - _M_size._M_size -= __detail::__to_unsigned_like(__d); - else - _M_size._M_size += __detail::__to_unsigned_like(-__d); - } - else - ranges::advance(_M_begin, __n, _M_end); + _M_size._M_size -= __detail::__to_unsigned_like(__d); return *this; } }; diff --git a/libstdc++-v3/testsuite/std/ranges/subrange/lwg3433.cc b/libstdc++-v3/testsuite/std/ranges/subrange/lwg3433.cc new file mode 100644 index 00000000000..9eb04895ee7 --- /dev/null +++ b/libstdc++-v3/testsuite/std/ranges/subrange/lwg3433.cc @@ -0,0 +1,96 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include +#include + +using __gnu_test::bidirectional_iterator_wrapper; +using __gnu_test::forward_iterator_wrapper; +using __gnu_test::test_range; +using __gnu_test::test_sized_range; +using __gnu_test::test_sized_range_sized_sent; + +namespace ranges = std::ranges; + +template +void +test01() +{ + int x[] = {1,2,3,4,5}; + Container r{x}; + ranges::subrange sr = r; + constexpr bool sized_range_p = ranges::sized_range; + constexpr bool bidirectional_p = ranges::bidirectional_range; + VERIFY( ranges::equal(sr, (int[]){1,2,3,4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 5 ); + + sr = sr.next(); + VERIFY( ranges::equal(sr, (int[]){2,3,4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 4 ); + + sr = std::move(sr).next(2); + VERIFY( ranges::equal(sr, (int[]){4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 2 ); + + if constexpr (bidirectional_p) + { + sr = sr.prev(2); + VERIFY( ranges::equal(sr, (int[]){2,3,4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 4 ); + + sr = sr.prev(); + VERIFY( ranges::equal(sr, (int[]){1,2,3,4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 5 ); + } + else + sr = r; + + sr.advance(1); + VERIFY( ranges::equal(sr, (int[]){2,3,4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 4 ); + + if constexpr (bidirectional_p) + { + sr.advance(-1); + VERIFY( ranges::equal(sr, (int[]){1,2,3,4,5}) ); + if constexpr (sized_range_p) + VERIFY( sr.size() == 5 ); + } +} + +int +main() +{ + test01>(); + test01>(); + test01>(); + + test01>(); + test01>(); + test01>(); +}