public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: libstdc++@gcc.gnu.org, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] libstdc++: Fix access issue in iota_view::_Sentinel [PR100690]
Date: Thu, 20 May 2021 13:44:36 -0400	[thread overview]
Message-ID: <20210520174436.1217207-1-ppalka@redhat.com> (raw)

Tested on x86_64-pc-linux-gnu, does this look OK for trunk/11/10?

libstdc++-v3/ChangeLog:

	PR libstdc++/100690
	* include/std/ranges (iota_view::_Sentinel::_M_distance_from):
	Split out into this member function from ...
	(iota_view::_Sentinel::operator-): ... here, for sake of access
	checking.
	* testsuite/std/ranges/iota/iota_view.cc:
---
 libstdc++-v3/include/std/ranges                     |  8 ++++++--
 libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc | 11 +++++++++++
 2 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/std/ranges b/libstdc++-v3/include/std/ranges
index 704f924557c..76add252ca6 100644
--- a/libstdc++-v3/include/std/ranges
+++ b/libstdc++-v3/include/std/ranges
@@ -499,6 +499,10 @@ namespace ranges
 	_M_equal(const _Iterator& __x) const
 	{ return __x._M_value == _M_bound; }
 
+	constexpr auto
+	_M_distance_from(const _Iterator& __x) const
+	{ return _M_bound - __x._M_value; }
+
 	_Bound _M_bound = _Bound();
 
       public:
@@ -515,12 +519,12 @@ namespace ranges
 	friend constexpr iter_difference_t<_Winc>
 	operator-(const _Iterator& __x, const _Sentinel& __y)
 	  requires sized_sentinel_for<_Bound, _Winc>
-	{ return __x._M_value - __y._M_bound; }
+	{ return -__y._M_distance_from(__x); }
 
 	friend constexpr iter_difference_t<_Winc>
 	operator-(const _Sentinel& __x, const _Iterator& __y)
 	  requires sized_sentinel_for<_Bound, _Winc>
-	{ return -(__y - __x); }
+	{ return __x._M_distance_from(__y); }
       };
 
       _Winc _M_value = _Winc();
diff --git a/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc b/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
index be8695120ad..362ef1f7f78 100644
--- a/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
+++ b/libstdc++-v3/testsuite/std/ranges/iota/iota_view.cc
@@ -80,6 +80,16 @@ test04()
 // Verify we optimize away the 'bound' data member of an unbounded iota_view.
 static_assert(sizeof(std::ranges::iota_view<char>) == 1);
 
+void
+test05()
+{
+  // PR libstdc++/100690
+  int x[] = {42, 42, 42};
+  auto r = std::views::iota(std::ranges::begin(x), std::ranges::cbegin(x) + 3);
+  VERIFY( r.end() - r.begin() == 3 );
+  VERIFY( r.begin() - r.end() == -3 );
+}
+
 int
 main()
 {
@@ -87,4 +97,5 @@ main()
   test02();
   test03();
   test04();
+  test05();
 }
-- 
2.32.0.rc0


             reply	other threads:[~2021-05-20 17:44 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-20 17:44 Patrick Palka [this message]
2021-05-20 18:37 ` Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210520174436.1217207-1-ppalka@redhat.com \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).