Skip to content

<xutility>: ranges::find with unreachable_sentinel / __std_find_trivial_unsized_1 gives wrong result #4449

@Ilendur

Description

@Ilendur

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

>type repro.cpp
#include <cstdint>
#include <array>
#include <algorithm>
#include <print>

int main()
{
    std::array<std::uint8_t, 0x10000> arr;

    std::ranges::fill(arr, std::uint8_t{1});
    arr.back() = 0;

    std::println("begin: {}", static_cast<void*>(std::to_address(std::begin(arr))));
    std::println("end:   {}", static_cast<void*>(std::to_address(std::end(arr))));

    for (auto it = std::begin(arr); it != std::end(arr); ++it) {
        auto where = std::ranges::find(it, std::unreachable_sentinel, 0);
        auto dist = std::distance(it, where);
        if (dist < 0)
            std::println("{:p} = {}", static_cast<void*>(std::to_address(where)), dist);
    }
}

>cl /EHsc /W4 /WX /std:c++latest .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.40.33521 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

/std:c++latest is provided as a preview of language features from the latest C++
working draft, and we're eager to hear about bugs and suggestions for improvements.
However, note that these features are provided as-is without support, and subject
to changes or removal as the working draft evolves. See
https://go.microsoft.com/fwlink/?linkid=2045807 for details.

repro.cpp
Microsoft (R) Incremental Linker Version 14.40.33521.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repro.exe
repro.obj

>.\repro.exe
begin: 0x6802effbb0
end:   0x6802f0fbb0
0x6802effba0 = -16
0x6802effba6 = -11
0x6802effba6 = -12
0x6802effba6 = -13
0x6802effba6 = -14
0x6802effba6 = -15
0x6802effba6 = -16
0x6802effba6 = -17
0x6802effba6 = -18
0x6802effba6 = -19
0x6802effba6 = -20
0x6802effba6 = -21
0x6802effba6 = -22
0x6802effba6 = -23
0x6802effba6 = -24
0x6802effba6 = -25

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

Microsoft Visual Studio Community 2022
Version 17.9.2

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!rangesC++20/23 ranges

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions