diff mbox series

libstdc++: Fix subrange::advance and subrange::prev (LWG 3433)

Message ID 20200427213432.3502548-1-ppalka@redhat.com
State New
Headers show
Series libstdc++: Fix subrange::advance and subrange::prev (LWG 3433) | expand

Commit Message

Patrick Palka April 27, 2020, 9:34 p.m. UTC
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

Comments

Jonathan Wakely April 28, 2020, 4:23 p.m. UTC | #1
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.
diff mbox series

Patch

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.
+	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..2b01729b40f
--- /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
+// <http://www.gnu.org/licenses/>.
+
+// { dg-options "-std=gnu++2a" }
+// { dg-do run { target c++2a } }
+
+#include <algorithm>
+#include <ranges>
+#include <testsuite_iterators.h>
+#include <testsuite_hooks.h>
+
+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<typename Container>
+void
+test01()
+{
+  int x[] = {1,2,3,4,5};
+  Container r{x};
+  ranges::subrange sr = r;
+  constexpr bool sized_range_p = ranges::sized_range<decltype(sr)>;
+  constexpr bool bidirectional_p = ranges::bidirectional_range<decltype(sr)>;
+  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(1);
+      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<test_sized_range_sized_sent<int, bidirectional_iterator_wrapper>>();
+  test01<test_sized_range<int, bidirectional_iterator_wrapper>>();
+  test01<test_range<int, bidirectional_iterator_wrapper>>();
+
+  test01<test_sized_range_sized_sent<int, forward_iterator_wrapper>>();
+  test01<test_sized_range<int, forward_iterator_wrapper>>();
+  test01<test_range<int, forward_iterator_wrapper>>();
+}