public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r11-9837] libstdc++: Prevent -Wstringop-overread warning in std::deque [PR100516]
Date: Tue, 12 Apr 2022 21:13:48 +0000 (GMT)	[thread overview]
Message-ID: <20220412211348.141AB3858D28@sourceware.org> (raw)

https://gcc.gnu.org/g:573bb865df967b420aba5382a5987b8865f83dc0

commit r11-9837-g573bb865df967b420aba5382a5987b8865f83dc0
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Thu Jan 27 22:31:26 2022 +0000

    libstdc++: Prevent -Wstringop-overread warning in std::deque [PR100516]
    
    The compiler warns about the loop in deque::_M_range_initialize because
    it doesn't know that the number of nodes has already been correctly
    sized to match the size of the input. Use __builtin_unreachable to tell
    it that the loop will never be entered if the number of elements is
    smaller than a single node.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/100516
            * include/bits/deque.tcc (_M_range_initialize<ForwardIterator>):
            Add __builtin_unreachable to loop.
            * testsuite/23_containers/deque/100516.cc: New test.
    
    (cherry picked from commit eae41b4d2cc30327f9f15c7390438c46aa09ed3f)

Diff:
---
 libstdc++-v3/include/bits/deque.tcc                  |  3 +++
 libstdc++-v3/testsuite/23_containers/deque/100516.cc | 14 ++++++++++++++
 2 files changed, 17 insertions(+)

diff --git a/libstdc++-v3/include/bits/deque.tcc b/libstdc++-v3/include/bits/deque.tcc
index db532e3c585..13c0f8a2be3 100644
--- a/libstdc++-v3/include/bits/deque.tcc
+++ b/libstdc++-v3/include/bits/deque.tcc
@@ -454,6 +454,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 		 __cur_node < this->_M_impl._M_finish._M_node;
 		 ++__cur_node)
 	      {
+		if (__n < _S_buffer_size())
+		  __builtin_unreachable(); // See PR 100516
+
 		_ForwardIterator __mid = __first;
 		std::advance(__mid, _S_buffer_size());
 		std::__uninitialized_copy_a(__first, __mid, *__cur_node,
diff --git a/libstdc++-v3/testsuite/23_containers/deque/100516.cc b/libstdc++-v3/testsuite/23_containers/deque/100516.cc
new file mode 100644
index 00000000000..ef32ae10545
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/deque/100516.cc
@@ -0,0 +1,14 @@
+// { dg-options "-O2 -Wstringop-overread" }
+// { dg-do compile { target c++11 } }
+
+// Bug 100516
+// Unexpected -Wstringop-overread in deque<char> initialization from empty
+// initializer_list
+
+#include <deque>
+
+void f()
+{
+    std::initializer_list<char> il{};
+    std::deque<char>{il};
+}


                 reply	other threads:[~2022-04-12 21:13 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=20220412211348.141AB3858D28@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@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).