From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id 06EC4398E46F; Thu, 17 Sep 2020 16:54:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 06EC4398E46F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1600361667; bh=G3ojVN4Xkp7sHrPuaOSKRU8xxYseAyaOXkmfdq8eNgw=; h=From:To:Subject:Date:From; b=HqsVksoHBKFIweq4zwEXgdMUPUvt3JeJ5j+TUhxFFl9e9CRCiqE0MWzFje4uIOuoe sVxipImtQ7kX25NXslBGG5hLdVovRKlFyJy1koKvsDYgfQqfSbJ+GIEpvh2/t15e32 uMK/XdZskKpF7+CCj2Z/PsNkS7BACOGXWR3E74wM= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc(refs/vendors/redhat/heads/gcc-8-branch)] libstdc++: Fix std::to_address for debug iterators (PR 93960) X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/vendors/redhat/heads/gcc-8-branch X-Git-Oldrev: 33adf39efb59da495dd8879a0a6bc5c919497d48 X-Git-Newrev: f45afea8584e3d12fb44c63f07af2b2f32a3e88a Message-Id: <20200917165427.06EC4398E46F@sourceware.org> Date: Thu, 17 Sep 2020 16:54:27 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 17 Sep 2020 16:54:27 -0000 https://gcc.gnu.org/g:f45afea8584e3d12fb44c63f07af2b2f32a3e88a commit f45afea8584e3d12fb44c63f07af2b2f32a3e88a Author: Jonathan Wakely Date: Fri Apr 3 12:00:07 2020 +0100 libstdc++: Fix std::to_address for debug iterators (PR 93960) It should be valid to use std::to_address on a past-the-end iterator, but the debug mode iterators do a check for dereferenceable in their operator->(). That check is generally useful, so rather than remove it this changes std::__to_address to identify a debug mode iterator and use base().operator->() to skip the check. Backport from mainline 2020-04-03 Jonathan Wakely PR libstdc++/93960 * include/bits/ptr_traits.h (__to_address): Add special case for debug iterators, to avoid dereferenceable check. * testsuite/20_util/to_address/1_neg.cc: Adjust dg-error line number. * testsuite/20_util/to_address/debug.cc: New test. Diff: --- libstdc++-v3/ChangeLog | 11 +++++++ libstdc++-v3/include/bits/ptr_traits.h | 11 ++++++- libstdc++-v3/testsuite/20_util/to_address/1_neg.cc | 2 +- libstdc++-v3/testsuite/20_util/to_address/debug.cc | 36 ++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 1d6f972584a..495f6809729 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,14 @@ +2020-04-03 Jonathan Wakely + + Backport from mainline + 2020-04-03 Jonathan Wakely + + PR libstdc++/93960 + * include/bits/ptr_traits.h (__to_address): Add special case for debug + iterators, to avoid dereferenceable check. + * testsuite/20_util/to_address/1_neg.cc: Adjust dg-error line number. + * testsuite/20_util/to_address/debug.cc: New test. + 2020-03-04 Release Manager * GCC 8.4.0 released. diff --git a/libstdc++-v3/include/bits/ptr_traits.h b/libstdc++-v3/include/bits/ptr_traits.h index 11b6056370a..88261de02d6 100644 --- a/libstdc++-v3/include/bits/ptr_traits.h +++ b/libstdc++-v3/include/bits/ptr_traits.h @@ -34,6 +34,10 @@ #include +#if __cplusplus > 201703L +namespace __gnu_debug { struct _Safe_iterator_base; } +#endif + namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION @@ -169,7 +173,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template constexpr auto __to_address(const _Ptr& __ptr, _None...) noexcept - { return std::__to_address(__ptr.operator->()); } + { + if constexpr (is_base_of_v<__gnu_debug::_Safe_iterator_base, _Ptr>) + return std::__to_address(__ptr.base().operator->()); + else + return std::__to_address(__ptr.operator->()); + } /** * @brief Obtain address referenced by a pointer to an object diff --git a/libstdc++-v3/testsuite/20_util/to_address/1_neg.cc b/libstdc++-v3/testsuite/20_util/to_address/1_neg.cc index e5681de588f..3346253f2a8 100644 --- a/libstdc++-v3/testsuite/20_util/to_address/1_neg.cc +++ b/libstdc++-v3/testsuite/20_util/to_address/1_neg.cc @@ -17,7 +17,7 @@ // { dg-options "-std=gnu++2a" } // { dg-do compile { target c++2a } } -// { dg-error "not a function pointer" "" { target *-*-* } 153 } +// { dg-error "not a function pointer" "" { target *-*-* } 157 } #include diff --git a/libstdc++-v3/testsuite/20_util/to_address/debug.cc b/libstdc++-v3/testsuite/20_util/to_address/debug.cc new file mode 100644 index 00000000000..4555284416d --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/to_address/debug.cc @@ -0,0 +1,36 @@ +// 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 + +void +test01() +{ + __gnu_debug::vector v{1, 2, 3}; + auto p = std::to_address(v.end()); + VERIFY( p == v.data() + v.size() ); +} + +int +main() +{ + test01(); +}