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<icinga::String>; 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.
This commit is contained in:
Julian Brost 2025-03-10 09:28:33 +01:00
parent 067131aa51
commit 4114d31434

View File

@ -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);