From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 1F6C6382D3CA for ; Tue, 6 Dec 2022 21:36:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1F6C6382D3CA Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1670362618; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=ZQFV1ThtLvXbPvNv4use3XRujjFtUW5s/t90bjQuI7M=; b=BnLwwx9S/dMRsbI1v9F0C2kEkGU88DBoaNsARnPPVzNs9z961Gx1TYlU+mBTrDvFVqlRLP CkdnDmWc5VjGYme012Tn7z2amclFX2sSxLJVJeJMcvd99q84i83wZmJk1k2JRsmONUEMNa MjyD8+i+ENZPOA1x5r3EPVF6u+XvGDQ= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-65-XOZPCgiqOd2eLZLeABSDhA-1; Tue, 06 Dec 2022 16:36:57 -0500 X-MC-Unique: XOZPCgiqOd2eLZLeABSDhA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 56934101A528; Tue, 6 Dec 2022 21:36:57 +0000 (UTC) Received: from localhost (unknown [10.33.36.203]) by smtp.corp.redhat.com (Postfix) with ESMTP id 21332FD48; Tue, 6 Dec 2022 21:36:57 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Add hint to compiler about vector invariants [PR106434] Date: Tue, 6 Dec 2022 21:36:54 +0000 Message-Id: <20221206213654.239004-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-linux. Pushed to trunk. -- >8 -- 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. --- 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); -- 2.38.1