From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) by sourceware.org (Postfix) with ESMTP id D4413385BF92 for ; Fri, 3 Apr 2020 09:51:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D4413385BF92 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-245-U6xTDuH8Pfyk7S-F3EIbjg-1; Fri, 03 Apr 2020 05:51:42 -0400 X-MC-Unique: U6xTDuH8Pfyk7S-F3EIbjg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1C81C100550D; Fri, 3 Apr 2020 09:51:41 +0000 (UTC) Received: from localhost (unknown [10.33.36.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id B367460BF1; Fri, 3 Apr 2020 09:51:40 +0000 (UTC) Date: Fri, 3 Apr 2020 10:51:39 +0100 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix std::to_address for debug iterators (PR 93960) Message-ID: <20200403095139.GA949185@redhat.com> MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="ikeVEW9yuYc//A+q" Content-Disposition: inline X-Spam-Status: No, score=-28.3 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 03 Apr 2020 09:51:46 -0000 --ikeVEW9yuYc//A+q Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable 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. =09PR libstdc++/93960 =09* include/bits/ptr_traits.h (__to_address): Add special case for debug =09iterators, to avoid dereferenceable check. =09* testsuite/20_util/to_address/1_neg.cc: Adjust dg-error line number. =09* testsuite/20_util/to_address/debug.cc: New test. Tested powerpc64le-linux, committed to master. --ikeVEW9yuYc//A+q Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" Content-Transfer-Encoding: quoted-printable commit 24fe8c8e338e1c820d942b9c7b997a37705eb29e Author: Jonathan Wakely Date: Fri Apr 3 10:42:20 2020 +0100 libstdc++: Fix std::to_address for debug iterators (PR 93960) =20 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. =20 PR libstdc++/93960 * include/bits/ptr_traits.h (__to_address): Add special case fo= r debug iterators, to avoid dereferenceable check. * testsuite/20_util/to_address/1_neg.cc: Adjust dg-error line n= umber. * testsuite/20_util/to_address/debug.cc: New test. diff --git a/libstdc++-v3/include/bits/ptr_traits.h b/libstdc++-v3/include/= bits/ptr_traits.h index 429f7fde520..8fd91bf7bf2 100644 --- a/libstdc++-v3/include/bits/ptr_traits.h +++ b/libstdc++-v3/include/bits/ptr_traits.h @@ -34,6 +34,10 @@ =20 #include =20 +#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>) +=09return std::__to_address(__ptr.base().operator->()); + else +=09return std::__to_address(__ptr.operator->()); + } =20 /** * @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 ceb2141f10b..ee64a2281fb 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 @@ =20 // { dg-options "-std=3Dgnu++2a" } // { dg-do compile { target c++2a } } -// { dg-error "not a function pointer" "" { target *-*-* } 153 } +// { dg-error "not a function pointer" "" { target *-*-* } 157 } =20 #include =20 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=3Dgnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include + +void +test01() +{ + __gnu_debug::vector v{1, 2, 3}; + auto p =3D std::to_address(v.end()); + VERIFY( p =3D=3D v.data() + v.size() ); +} + +int +main() +{ + test01(); +} --ikeVEW9yuYc//A+q--