Skip to content

Avoid undefined behavior in string/vector_move test#10365

Merged
julianbrost merged 1 commit intomasterfrom
string-vector-move-test
Mar 10, 2025
Merged

Avoid undefined behavior in string/vector_move test#10365
julianbrost merged 1 commit intomasterfrom
string-vector-move-test

Conversation

@julianbrost
Copy link
Copy Markdown
Member

@julianbrost julianbrost commented Mar 10, 2025

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 problem was recently introduced with PR #10353.

closes #10363

Tests

Current master branch

Failed snapshot pipeline for 206d7cd: https://git.icinga.com/packages/icinga2/-/pipelines/35807 (not publicly accessible)

rpm-binary jobs failed on: Amazon Linux 2023; Fedora 39, 40, 41; RHEL 8, 9

This PR

Successful pipeline for 784867b: https://git.icinga.com/packages/icinga2/-/pipelines/35817 (not publicly accessible)

rpm-binary jobs are all now succeeding

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.
@julianbrost julianbrost added the core/build-fix Follow-up fix, not released yet label Mar 10, 2025
@cla-bot cla-bot bot added the cla/signed label Mar 10, 2025
@julianbrost julianbrost marked this pull request as draft March 10, 2025 08:39
@yhabteab
Copy link
Copy Markdown
Member

Err! GitHub PR races 😅! Then please close #10363 when merging this PR!

@julianbrost
Copy link
Copy Markdown
Member Author

Well, I didn't see your PR until after I opened this one :D

Any preference for either PR on your side? I'd say with your change, it's not that obvious why there's a second string inside the vector that isn't really used otherwise by the test.

@yhabteab
Copy link
Copy Markdown
Member

Any preference for either PR on your side?

+1 for this PR!

@Al2Klimov
Copy link
Copy Markdown
Member

If the GitLab pipelines differ from GHA, don't the latter miss any compiler flags or similar?

@julianbrost julianbrost marked this pull request as ready for review March 10, 2025 10:51
@julianbrost
Copy link
Copy Markdown
Member Author

Yes, there's probably a difference in compile flags. Though this PR is about getting rid of the undefined behavior.

@julianbrost julianbrost requested a review from yhabteab March 10, 2025 10:53
@Al2Klimov
Copy link
Copy Markdown
Member

Don't get me wrong, I'm also for getting rid off this UB, especially if it breaks our packaging. I'd just like to get notified about such ASAP – i.e. via GHA, not just when it's packaging time. For this I'd like us to first fix the GHA, so that it fails just like packaging does rn.

@julianbrost
Copy link
Copy Markdown
Member Author

Feel free to create a PR for that.

I'd like us to first fix the GHA

However, I don't see a reason why this should block this PR.

@julianbrost
Copy link
Copy Markdown
Member Author

I've created #10367 for the compiler flags.

@julianbrost julianbrost merged commit cefe1bc into master Mar 10, 2025
23 checks passed
@julianbrost julianbrost deleted the string-vector-move-test branch March 10, 2025 13:51
@Al2Klimov Al2Klimov added this to the 2.15.0 milestone Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla/signed core/build-fix Follow-up fix, not released yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants