From 784867b3f7c9736e4b49b78c51de2c59df4d7fa4 Mon Sep 17 00:00:00 2001 From: Julian Brost Date: Mon, 10 Mar 2025 09:28:33 +0100 Subject: [PATCH] Avoid undefined behavior in string/vector_move test vec[1] is equivalent to vec[vec.size()] at that point and thus not a valid element of the vector, making the use of operator[] undefined behavior here. With some compiler flags (like those used in package builds on RHEL and similar), the compiler (rightfully) aborts the program on this out of bounds access: 68/178 Test #68: base-base_string/vector_move ............................................***Failed 0.01 sec /usr/include/c++/14/bits/stl_vector.h:1130: std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::operator[](size_type) [with _Tp = icinga::String; _Alloc = std::allocator; reference = icinga::String&; size_type = long unsigned int]: Assertion '__n < this->size()' failed. Running 1 test case... unknown location(0): fatal error: in "base_string/vector_move": signal: SIGABRT (application abort requested) /builds/packages/icinga2/packaging/fedora/41/BUILD/icinga2-2.14.5+467.g206d7cda1-build/icinga2-2.14.5+467.g206d7cda1/test/base-string.cpp(120): last checkpoint *** 1 failure is detected in the test module "icinga2" This commit fixes this by taking the indirection through .data() and using plain pointer arithmetic instead. --- test/base-string.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/base-string.cpp b/test/base-string.cpp index 50c1f6af8..7a6b36264 100644 --- a/test/base-string.cpp +++ b/test/base-string.cpp @@ -117,7 +117,7 @@ BOOST_AUTO_TEST_CASE(vector_move) void *oldAddr = vec[0].GetData().data(); // Sanity check that the data buffer is actually allocated outside the icinga::String instance. - BOOST_CHECK(!(&vec[0] <= oldAddr && oldAddr < &vec[1])); + BOOST_CHECK(!(vec.data() <= oldAddr && oldAddr < vec.data() + vec.size())); // Force the vector to grow. vec.reserve(vec.capacity() + 1);