[libc++] Don't assume a valid range in find_if for empty inputs (#213752)
Iterators for empty ranges are sometimes implemented using a non-aligned sentinel value. Requiring proper alignment in that case (which is part of the valid range assumption) causes a hardening failure. Instead, don't make the assumption of a valid range when the input range is empty in find_if. (cherry picked from commit 83290d61f6ead40a5c38297a0442b63291ce7a48)
This commit is contained in:
1 parent
2eabe80bc7
commit
82fc843747
2 files changed
+91
-1
No files matched your search
@@ -24,11 +24,18 @@ _LIBCPP_BEGIN_NAMESPACE_STD
|
||||
template <class _Iter, class _Sent, class _Pred, class _Proj>
|
||||
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 _Iter
|
||||
__find_if(_Iter __first, _Sent __last, _Pred&& __pred, _Proj&& __proj) {
|
||||
// Don't make the valid range assumption for empty ranges since using unaligned sentinel
|
||||
// values to implement iterators of empty ranges is a common occurence.
|
||||
if (__first == __last)
|
||||
return __first;
|
||||
|
||||
std::__assume_valid_range(__first, __last);
|
||||
|
||||
for (; __first != __last; ++__first)
|
||||
do {
|
||||
if (std::__invoke(__pred, std::__invoke(__proj, *__first)))
|
||||
break;
|
||||
++__first;
|
||||
} while (__first != __last);
|
||||
return __first;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
||||
// See https://llvm.org/LICENSE.txt for license information.
|
||||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
// Some algorithms make alignment assumptions for optimization purposes. However, it is
|
||||
// fairly common for data structures to implement iterators for empty ranges using
|
||||
// sentinel values, which may not be properly aligned. This test ensures that we don't
|
||||
// make alignment assumptions for empty ranges, which would break that use case.
|
||||
|
||||
// UNSUPPORTED: c++03
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <ranges>
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
// Returns a pointer that doesn't point to any object at all and that is not suitably
|
||||
// aligned for T. This is the sort of sentinel value that user code sometimes uses to
|
||||
// represent an empty range.
|
||||
template <class T>
|
||||
T* unaligned_sentinel() {
|
||||
return reinterpret_cast<T*>(static_cast<std::uintptr_t>(1));
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void test() {
|
||||
T* first = unaligned_sentinel<T>();
|
||||
T* last = first;
|
||||
T value{};
|
||||
auto pred = [](T const&) { return true; };
|
||||
|
||||
assert(std::find(first, last, value) == last);
|
||||
assert(std::find_if(first, last, pred) == last);
|
||||
assert(std::find_if_not(first, last, pred) == last);
|
||||
assert(std::any_of(first, last, pred) == false);
|
||||
assert(std::all_of(first, last, pred) == true);
|
||||
assert(std::none_of(first, last, pred) == true);
|
||||
assert(std::remove(first, last, value) == last);
|
||||
assert(std::remove_if(first, last, pred) == last);
|
||||
|
||||
#if TEST_STD_VER >= 20
|
||||
// (iterator, sentinel) overloads
|
||||
assert(std::ranges::find(first, last, value) == last);
|
||||
assert(std::ranges::find_if(first, last, pred) == last);
|
||||
assert(std::ranges::find_if_not(first, last, pred) == last);
|
||||
assert(std::ranges::any_of(first, last, pred) == false);
|
||||
assert(std::ranges::all_of(first, last, pred) == true);
|
||||
assert(std::ranges::none_of(first, last, pred) == true);
|
||||
assert(std::ranges::remove(first, last, value).begin() == last);
|
||||
assert(std::ranges::remove_if(first, last, pred).begin() == last);
|
||||
|
||||
// (range) overloads
|
||||
std::ranges::subrange range(first, last);
|
||||
assert(std::ranges::find(range, value) == last);
|
||||
assert(std::ranges::find_if(range, pred) == last);
|
||||
assert(std::ranges::find_if_not(range, pred) == last);
|
||||
assert(std::ranges::any_of(range, pred) == false);
|
||||
assert(std::ranges::all_of(range, pred) == true);
|
||||
assert(std::ranges::none_of(range, pred) == true);
|
||||
assert(std::ranges::remove(range, value).begin() == last);
|
||||
assert(std::ranges::remove_if(range, pred).begin() == last);
|
||||
#endif
|
||||
}
|
||||
|
||||
struct alignas(16) Overaligned {
|
||||
int value = 0;
|
||||
friend bool operator==(Overaligned const& x, Overaligned const& y) { return x.value == y.value; }
|
||||
};
|
||||
|
||||
int main(int, char**) {
|
||||
test<int>();
|
||||
// not trivially equality comparable
|
||||
test<double>();
|
||||
test<Overaligned>();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user