[gcc r13-9486] libstdc++: Fix bogus -Wstringop-overflow in std::vector::insert [PR117983]
Jonathan Wakely
redi@gcc.gnu.org
Wed Apr 2 17:14:20 GMT 2025
https://gcc.gnu.org/g:8f633fe7c7d03ed6c0ce029a831037a6274a0882
commit r13-9486-g8f633fe7c7d03ed6c0ce029a831037a6274a0882
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Fri Mar 28 22:00:38 2025 +0000
libstdc++: Fix bogus -Wstringop-overflow in std::vector::insert [PR117983]
This was fixed on trunk by r15-4473-g3abe751ea86e34, but that isn't
suitable for backporting. Instead, just add another unreachable
condition in std::vector::_M_range_insert so the compiler knows this
memcpy doesn't use a length originating from a negative ptrdiff_t
converted to a very positive size_t.
libstdc++-v3/ChangeLog:
PR libstdc++/117983
* include/bits/vector.tcc (vector::_M_range_insert): Add
unreachable condition to tell the compiler begin() <= end().
* testsuite/23_containers/vector/modifiers/insert/117983.cc: New
test.
Reviewed-by: Tomasz Kamiński <tkaminsk@redhat.com>
(cherry picked from commit 878812b6f6905774ab37cb78903e3e11bf1c508c)
Diff:
---
libstdc++-v3/include/bits/vector.tcc | 2 ++
.../23_containers/vector/modifiers/insert/117983.cc | 17 +++++++++++++++++
2 files changed, 19 insertions(+)
diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc
index c762dbf798f6..ddbfde447f62 100644
--- a/libstdc++-v3/include/bits/vector.tcc
+++ b/libstdc++-v3/include/bits/vector.tcc
@@ -799,6 +799,8 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
// reachable.
pointer __old_start = this->_M_impl._M_start;
pointer __old_finish = this->_M_impl._M_finish;
+ if ((__old_finish - __old_start) < 0)
+ __builtin_unreachable();
const size_type __len =
_M_check_len(__n, "vector::_M_range_insert");
diff --git a/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/117983.cc b/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/117983.cc
new file mode 100644
index 000000000000..e6027a677eed
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/vector/modifiers/insert/117983.cc
@@ -0,0 +1,17 @@
+// { dg-options "-O3 -Werror=stringop-overflow" }
+// { dg-do compile }
+
+// PR libstdc++/117983
+// -Wstringop-overflow false positive for __builtin_memmove from vector::insert
+
+#include <vector>
+
+typedef std::vector<unsigned char> bytes;
+
+void push(bytes chunk, bytes& data) {
+ if (data.empty()) {
+ data.swap(chunk);
+ } else {
+ data.insert(data.end(), chunk.begin(), chunk.end());
+ }
+}
More information about the Libstdc++-cvs
mailing list