]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Prevent -Wstringop-overread warning in std::deque [PR100516]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 27 Jan 2022 22:31:26 +0000 (22:31 +0000)
committerJonathan Wakely <jwakely@redhat.com>
Tue, 12 Apr 2022 20:27:34 +0000 (21:27 +0100)
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)

libstdc++-v3/include/bits/deque.tcc
libstdc++-v3/testsuite/23_containers/deque/100516.cc [new file with mode: 0644]

index db532e3c585bbee086a8eb9a23315d3c0db2304c..13c0f8a2be3738e528fe60f9220bd1f01afeaced 100644 (file)
@@ -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 (file)
index 0000000..ef32ae1
--- /dev/null
@@ -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};
+}