-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
<xutility>
: ranges::find
with unreachable_sentinel
/ __std_find_trivial_unsized_1
gives wrong result
#4449
Comments
<xutility>
: ranges::find
with unreachable_sentinel
/ __std_find_trivial_unsized_1
gives wrong result
Thanks for the excellent report! I can repro this reliably:
#include <algorithm>
#include <cstdint>
#include <print>
using namespace std;
template <typename T>
void test() {
T arr[1024];
const int zero_idx = 512;
for (int offset = 1; offset < 40; ++offset) {
for (int start = 256; start < 256 + 64; ++start) {
ranges::fill(arr, T{1});
arr[start - offset] = T{0};
arr[zero_idx] = T{0};
const auto where = ranges::find(arr + start, unreachable_sentinel, T{0}) - arr;
if (where != zero_idx) {
println("FAIL! sizeof(T)={}; offset={:2}; start={}; where={}", sizeof(T), offset, start, where);
break;
}
}
}
}
int main() {
test<uint8_t>();
test<uint16_t>();
test<uint32_t>();
test<uint64_t>();
println("DONE");
}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Describe the bug
When using ranges::find on a range of integers with unreachable_sentinel as end iterator, sometimes it finds a value before the begin iterator. It only happens if _USE_STD_VECTOR_ALGORITHMS is 1, when it's using __std_find_trivial_unsized_1.
Command-line test case
Expected behavior
Expected to find the 0 at the end of the range instead of getting results before the start of the range.
STL version
Additional context
The bug only seems to occur at certain memory addresses, so with the test array placed on the stack, the code might not show wrong results every single run. Similar problem seems to occur with different integer sizes.
The text was updated successfully, but these errors were encountered: