From: Jonathan Wakely <jwakely@redhat.com>
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 [thread overview]
Message-ID: <20221206213654.239004-1-jwakely@redhat.com> (raw)
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
reply other threads:[~2022-12-06 21:37 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20221206213654.239004-1-jwakely@redhat.com \
--to=jwakely@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=libstdc++@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).