Skip to content
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

Don't memcmp(x, y, -1) from ranges::lexicographical_compare #3989

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stl/inc/algorithm
Original file line number Diff line number Diff line change
Expand Up @@ -10654,7 +10654,7 @@ namespace ranges {
constexpr bool _Is_sized2 = sized_sentinel_for<_Se2, _It2>;
if constexpr (!is_void_v<_Memcmp_classification_pred> && _Sized_or_unreachable_sentinel_for<_Se1, _It1>
&& _Sized_or_unreachable_sentinel_for<_Se2, _It2> && same_as<_Pj1, identity>
&& same_as<_Pj2, identity>) {
&& same_as<_Pj2, identity> && (_Is_sized1 || _Is_sized2)) {
if (!_STD is_constant_evaluated()) {
size_t _Num1;
if constexpr (_Is_sized1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,40 +194,46 @@ struct instantiator {
empty1.begin(), empty1.end(), empty2.begin(), empty2.end(), less{}, get_first, get_second);
assert(!result);
}
{ // Validate memcmp case
unsigned char arr1[3]{0, 1, 2};
unsigned char arr2[3]{0, 1, 3};
assert(lexicographical_compare(arr1, arr2));
arr2[2] = 2;
assert(!lexicographical_compare(arr1, arr2));
arr2[2] = 1;
assert(!lexicographical_compare(arr1, arr2));
}
{ // Validate memcmp + unreachable_sentinel cases
unsigned char arr1[3]{0, 1, 2};
unsigned char arr2[3]{0, 1, 3};

assert(lexicographical_compare(begin(arr1), end(arr1), begin(arr1), unreachable_sentinel));
assert(!lexicographical_compare(begin(arr1), unreachable_sentinel, begin(arr1), end(arr1)));

assert(lexicographical_compare(begin(arr1), unreachable_sentinel, begin(arr2), unreachable_sentinel));
assert(lexicographical_compare(begin(arr1), unreachable_sentinel, begin(arr2), end(arr2)));
assert(lexicographical_compare(begin(arr1), end(arr1), begin(arr2), unreachable_sentinel));
arr2[2] = 2;
assert(!lexicographical_compare(begin(arr1), unreachable_sentinel, begin(arr2), end(arr2)));
assert(lexicographical_compare(begin(arr1), end(arr1), begin(arr2), unreachable_sentinel));
arr2[2] = 1;
assert(!lexicographical_compare(begin(arr1), unreachable_sentinel, begin(arr2), unreachable_sentinel));
assert(!lexicographical_compare(begin(arr1), unreachable_sentinel, begin(arr2), end(arr2)));
assert(!lexicographical_compare(begin(arr1), end(arr1), begin(arr2), unreachable_sentinel));
}
}
};

constexpr void concrete_tests() {
using ranges::lexicographical_compare, ranges::begin, ranges::end;

{ // Validate memcmp case
unsigned char arr1[3]{0, 1, 2};
unsigned char arr2[3]{0, 1, 3};
assert(lexicographical_compare(arr1, arr2));
arr2[2] = 2;
assert(!lexicographical_compare(arr1, arr2));
arr2[2] = 1;
assert(!lexicographical_compare(arr1, arr2));
}
{ // Validate memcmp + unreachable_sentinel cases
unsigned char arr1[3]{0, 1, 2};
unsigned char arr2[3]{0, 1, 3};

assert(lexicographical_compare(begin(arr1), end(arr1), begin(arr1), unreachable_sentinel));
assert(!lexicographical_compare(begin(arr1), unreachable_sentinel, begin(arr1), end(arr1)));

assert(lexicographical_compare(begin(arr1), unreachable_sentinel, begin(arr2), unreachable_sentinel));
assert(lexicographical_compare(begin(arr1), unreachable_sentinel, begin(arr2), end(arr2)));
assert(lexicographical_compare(begin(arr1), end(arr1), begin(arr2), unreachable_sentinel));
arr2[2] = 2;
assert(!lexicographical_compare(begin(arr1), unreachable_sentinel, begin(arr2), end(arr2)));
assert(lexicographical_compare(begin(arr1), end(arr1), begin(arr2), unreachable_sentinel));
arr2[2] = 1;
assert(!lexicographical_compare(begin(arr1), unreachable_sentinel, begin(arr2), unreachable_sentinel));
assert(!lexicographical_compare(begin(arr1), unreachable_sentinel, begin(arr2), end(arr2)));
assert(!lexicographical_compare(begin(arr1), end(arr1), begin(arr2), unreachable_sentinel));
}
}

#ifdef TEST_EVERYTHING
int main() {
// No constexpr tests here: we hit the constexpr step limits too quickly.
test_in_in<instantiator, const P, const P>();
concrete_tests();
}
#else // ^^^ test all range combinations / test only interesting combinations vvv
template <test::ProxyRef IsProxy>
Expand All @@ -243,6 +249,8 @@ constexpr void run_tests() {
instantiator::call<range_type<ProxyRef::yes>, range_type<ProxyRef::no>>();
instantiator::call<range_type<ProxyRef::no>, range_type<ProxyRef::yes>>();
instantiator::call<range_type<ProxyRef::no>, range_type<ProxyRef::no>>();

concrete_tests();
}

int main() {
Expand Down