From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 65449 invoked by alias); 27 Feb 2020 13:06:54 -0000 Mailing-List: contact libstdc++-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libstdc++-owner@gcc.gnu.org Received: (qmail 64511 invoked by uid 89); 27 Feb 2020 13:06:46 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-18.2 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 spammy= X-HELO: us-smtp-delivery-1.mimecast.com Received: from us-smtp-2.mimecast.com (HELO us-smtp-delivery-1.mimecast.com) (207.211.31.81) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 27 Feb 2020 13:06:44 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1582808802; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type; bh=W71gUmUOCw6C5TGp8c9va/18PYrm/wdemkdSgfHtmPc=; b=CKqnjZjYVEC31gjQMjA3SpW7awCuow8hTcVVftoP0rMbTnWP7HZz2unrvGlmcRDDhTvZiE o7/BZSWorqiHv9jAUznh/DxdPLSn9pF9KhklAw/pdf1h7VpmiCxPlNGpTmzZYyHMFAJUiY 99bQOITr29APcJQ5xErtrXDcczIcZ+Y= 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-269-Hj4iYjV6MRa_GP1I5dTAag-1; Thu, 27 Feb 2020 08:06:38 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7F89410CE780; Thu, 27 Feb 2020 13:06:37 +0000 (UTC) Received: from localhost (unknown [10.33.36.184]) by smtp.corp.redhat.com (Postfix) with ESMTP id 27B6D10027BA; Thu, 27 Feb 2020 13:06:37 +0000 (UTC) Date: Thu, 27 Feb 2020 13:06:00 -0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH] Improvements to valid range checks in debug mode Message-ID: <20200227130636.GV9441@redhat.com> MIME-Version: 1.0 X-Clacks-Overhead: GNU Terry Pratchett X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="tpZe61tYkA9f+p/0" Content-Disposition: inline X-IsSubscribed: yes X-SW-Source: 2020-02/txt/msg00199.txt.bz2 --tpZe61tYkA9f+p/0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-length: 288 These should wait for stage 1 but I'm posting them now for comment. With the change to __gnu_debug::__valid_range we now get a debug assertion for: std::string s; std::min_element(std::string::iterator{}, s.end()); where previously it would just crash with undefined behaviour. --tpZe61tYkA9f+p/0 Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="patch.txt" Content-Transfer-Encoding: quoted-printable Content-length: 6915 commit 77a610b7e88635ee7c63d82cc30fad9c80abebea Author: Jonathan Wakely Date: Thu Feb 27 11:17:31 2020 +0000 libstdc++: Minor optimization for min/max/minmax =20=20=20=20 By calling the internal __min_element (or __max_element or __minmax_element) function directly we avoid a function call and the valid range checks that are redundant when the range is defined by an initializer_list. =20=20=20=20 * include/bits/stl_algo.h (min(initializer_list)) (min(initializer_list, Compare)): Call __min_element directl= y to avoid redundant debug checks for valid ranges. (max(initializer_list), max(initializer_list, Compare)): Likewise, for __max_element. (minmax(initializer_list), minmax(initializer_list, Compa= re)): Likewise, for __minmax_element. diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bi= ts/stl_algo.h index 6503d1518d3..d5eed9c47f6 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -3543,38 +3543,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __gnu_cxx::__ops::__iter_comp_iter(__comp)); } =20 - // N2722 + DR 915. - template - _GLIBCXX14_CONSTEXPR - inline _Tp - min(initializer_list<_Tp> __l) - { return *std::min_element(__l.begin(), __l.end()); } - - template - _GLIBCXX14_CONSTEXPR - inline _Tp - min(initializer_list<_Tp> __l, _Compare __comp) - { return *std::min_element(__l.begin(), __l.end(), __comp); } - - template - _GLIBCXX14_CONSTEXPR - inline _Tp - max(initializer_list<_Tp> __l) - { return *std::max_element(__l.begin(), __l.end()); } - - template - _GLIBCXX14_CONSTEXPR - inline _Tp - max(initializer_list<_Tp> __l, _Compare __comp) - { return *std::max_element(__l.begin(), __l.end(), __comp); } - template _GLIBCXX14_CONSTEXPR inline pair<_Tp, _Tp> minmax(initializer_list<_Tp> __l) { + __glibcxx_requires_irreflexive(__l.begin(), __l.end); pair __p =3D - std::minmax_element(__l.begin(), __l.end()); + std::__minmax_element(__l.begin(), __l.end(), + __gnu_cxx::__ops::__iter_less_iter()); return std::make_pair(*__p.first, *__p.second); } =20 @@ -3583,8 +3560,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION inline pair<_Tp, _Tp> minmax(initializer_list<_Tp> __l, _Compare __comp) { + __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end, __comp); pair __p =3D - std::minmax_element(__l.begin(), __l.end(), __comp); + std::__minmax_element(__l.begin(), __l.end(), + __gnu_cxx::__ops::__iter_comp_iter(__comp)); return std::make_pair(*__p.first, *__p.second); } =20 @@ -3959,7 +3938,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION for (_RandomAccessIterator __i =3D __first + 1; __i !=3D __last; ++_= _i) std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first))); } -#endif +#endif // USE C99_STDINT =20 #endif // C++11 =20 @@ -5902,6 +5881,49 @@ _GLIBCXX_BEGIN_NAMESPACE_ALGO __gnu_cxx::__ops::__iter_comp_iter(__comp)); } =20 +#if __cplusplus >=3D 201103L + // N2722 + DR 915. + template + _GLIBCXX14_CONSTEXPR + inline _Tp + min(initializer_list<_Tp> __l) + { + __glibcxx_requires_irreflexive(__l.begin(), __l.end); + return *_GLIBCXX_STD_A::__min_element(__l.begin(), __l.end(), + __gnu_cxx::__ops::__iter_less_iter()); + } + + template + _GLIBCXX14_CONSTEXPR + inline _Tp + min(initializer_list<_Tp> __l, _Compare __comp) + { + __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end, __comp); + return *_GLIBCXX_STD_A::__min_element(__l.begin(), __l.end(), + __gnu_cxx::__ops::__iter_comp_iter(__comp)); + } + + template + _GLIBCXX14_CONSTEXPR + inline _Tp + max(initializer_list<_Tp> __l) + { + __glibcxx_requires_irreflexive(__l.begin(), __l.end); + return *_GLIBCXX_STD_A::__max_element(__l.begin(), __l.end(), + __gnu_cxx::__ops::__iter_less_iter()); + } + + template + _GLIBCXX14_CONSTEXPR + inline _Tp + max(initializer_list<_Tp> __l, _Compare __comp) + { + __glibcxx_requires_irreflexive_pred(__l.begin(), __l.end, __comp); + return *_GLIBCXX_STD_A::__max_element(__l.begin(), __l.end(), + __gnu_cxx::__ops::__iter_comp_iter(__comp)); + } +#endif // C++11 + #if __cplusplus >=3D 201402L /// Reservoir sampling algorithm. template Date: Thu Feb 27 11:20:54 2020 +0000 libstdc++: Improve check for valid forward iterator range =20=20=20=20 Since C++14 we can assume that value-initialized forward iterators are not part of a valid range (except the special case of an empty range defined by two value-initialized iterators). =20=20=20=20 This covers the case of null pointers which was checked by the overload for input iterators, but also handles non-pointers. =20=20=20=20 * include/debug/helper_functions.h (__valid_range_aux): Add ove= rload for forward iterators that checks for value-initialized iterato= rs. diff --git a/libstdc++-v3/include/debug/helper_functions.h b/libstdc++-v3/i= nclude/debug/helper_functions.h index 251582d5922..3e016c7643e 100644 --- a/libstdc++-v3/include/debug/helper_functions.h +++ b/libstdc++-v3/include/debug/helper_functions.h @@ -161,6 +161,28 @@ namespace __gnu_debug || (!__check_singular(__first) && !__check_singular(__last)); } =20 +#if __cplusplus >=3D 201402L + template + constexpr bool + __valid_range_aux(_ForwardIterator __first, _ForwardIterator __last, + std::forward_iterator_tag) + { + if (__first =3D=3D __last) + return true; + + // Value-initialized forward iterators cannot form a non-empty range. + if (__first =3D=3D _ForwardIterator() || __last =3D=3D _ForwardItera= tor()) + return false; + + // Hook for checking if safe iterators are singular. + if (__check_singular_aux(std::__addressof(__first)) + || __check_singular_aux(std::__addressof(__last))) + return false; + + return true; + } +#endif + template _GLIBCXX_CONSTEXPR inline bool @@ -168,7 +190,7 @@ namespace __gnu_debug std::random_access_iterator_tag) { return - __valid_range_aux(__first, __last, std::input_iterator_tag()) + __valid_range_aux(__first, __last, std::forward_iterator_tag()) && __first <=3D __last; } =20 --tpZe61tYkA9f+p/0--