From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 0A0F2382D3CE; Tue, 6 Dec 2022 21:36:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0A0F2382D3CE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670362583; bh=m8VSpD6DSUdkOGUNhepYlN7xXHoKseeUtXLk0RqD1HQ=; h=From:To:Subject:Date:From; b=vESeff9mtTzSuC4F0KE3+PErt/RuhTl1LjAxkgvk16zHnid75wJKCvl/I/wi5MP6I QdeQwsv2WSsjGJ8VujhzF2p48v3KEZ3fDRDPFn8I4PNDWTD5EqgiCpb7b0KlHK/w4N vk02cmLCApv77lfcykBPTu3wQ9ZhZCwaC6TqRYK4= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-4525] libstdc++: Add hint to compiler about vector invariants [PR106434] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/master X-Git-Oldrev: af177d7280668e5b21560165dc828754618e6621 X-Git-Newrev: 4ba94abf147fe7778a7541849ce27cafee74df9b Message-Id: <20221206213623.0A0F2382D3CE@sourceware.org> Date: Tue, 6 Dec 2022 21:36:23 +0000 (GMT) List-Id: https://gcc.gnu.org/g:4ba94abf147fe7778a7541849ce27cafee74df9b commit r13-4525-g4ba94abf147fe7778a7541849ce27cafee74df9b Author: Jonathan Wakely Date: Mon Dec 5 12:53:42 2022 +0000 libstdc++: Add hint to compiler about vector invariants [PR106434] The PR shows a bogus warning where jump threading generates code for the undefined case that the insertion point is a value-initialized iterator but _M_finish and _M_end_of_storage are unequal (so at least one must be non-null). Using __builtin_unreachable() removes the bogus warning. Also add an assertion to diagnose undefined misuses of a null iterator here, so we don't just silently optimize that undefined code to something unsafe. libstdc++-v3/ChangeLog: PR c++/106434 * include/bits/vector.tcc (insert(const_iterator, const T&)): Add assertion and optimization hint that the iterator for the insertion point must be non-null. Diff: --- libstdc++-v3/include/bits/vector.tcc | 40 +++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc index 27ef1a4ee7f..8ae79ffc7af 100644 --- a/libstdc++-v3/include/bits/vector.tcc +++ b/libstdc++-v3/include/bits/vector.tcc @@ -139,26 +139,32 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER { const size_type __n = __position - begin(); if (this->_M_impl._M_finish != this->_M_impl._M_end_of_storage) - if (__position == end()) - { - _GLIBCXX_ASAN_ANNOTATE_GROW(1); - _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, - __x); - ++this->_M_impl._M_finish; - _GLIBCXX_ASAN_ANNOTATE_GREW(1); - } - else - { + { + __glibcxx_assert(__position != const_iterator()); + if (!(__position != const_iterator())) + __builtin_unreachable(); // PR 106434 + + if (__position == end()) + { + _GLIBCXX_ASAN_ANNOTATE_GROW(1); + _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish, + __x); + ++this->_M_impl._M_finish; + _GLIBCXX_ASAN_ANNOTATE_GREW(1); + } + else + { #if __cplusplus >= 201103L - const auto __pos = begin() + (__position - cbegin()); - // __x could be an existing element of this vector, so make a - // copy of it before _M_insert_aux moves elements around. - _Temporary_value __x_copy(this, __x); - _M_insert_aux(__pos, std::move(__x_copy._M_val())); + const auto __pos = begin() + (__position - cbegin()); + // __x could be an existing element of this vector, so make a + // copy of it before _M_insert_aux moves elements around. + _Temporary_value __x_copy(this, __x); + _M_insert_aux(__pos, std::move(__x_copy._M_val())); #else - _M_insert_aux(__position, __x); + _M_insert_aux(__position, __x); #endif - } + } + } else #if __cplusplus >= 201103L _M_realloc_insert(begin() + (__position - cbegin()), __x);