From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 308C0385E455; Fri, 21 May 2021 03:39:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 308C0385E455 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-958] libstdc++: Fix access issue in iota_view::_Sentinel [PR100690] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: ea34e2edd3d7ab245d1f57a1487c10587f324ec6 X-Git-Newrev: 317a38cd468d565dc8ce45c6da0dbccf38808f70 Message-Id: <20210521033946.308C0385E455@sourceware.org> Date: Fri, 21 May 2021 03:39:44 +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: Fri, 21 May 2021 03:39:46 -0000 https://gcc.gnu.org/g:317a38cd468d565dc8ce45c6da0dbccf38808f70 commit r12-958-g317a38cd468d565dc8ce45c6da0dbccf38808f70 Author: Patrick Palka Date: Thu May 20 23:39:05 2021 -0400 libstdc++: Fix access issue in iota_view::_Sentinel [PR100690] libstdc++-v3/ChangeLog: PR libstdc++/100690 * include/std/ranges (iota_view::_Sentinel::_M_distance_from): Split out this member function from ... (iota_view::_Sentinel::operator-): ... here, for sake of access control. * testsuite/std/ranges/iota/iota_view.cc (test05): New test. Diff: --- 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 0588bebb351..767a65c5822 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) == 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(); }