UB/Crash in entt::insertion_sort (#167)
This commit is contained in:
@@ -54,19 +54,21 @@ struct insertion_sort final {
|
||||
*/
|
||||
template<typename It, typename Compare = std::less<>>
|
||||
void operator()(It first, It last, Compare compare = Compare{}) const {
|
||||
auto it = first + 1;
|
||||
if(first != last) {
|
||||
auto it = first + 1;
|
||||
|
||||
while(it != last) {
|
||||
auto value = *it;
|
||||
auto pre = it;
|
||||
while(it != last) {
|
||||
auto value = *it;
|
||||
auto pre = it;
|
||||
|
||||
while(pre != first && compare(value, *(pre-1))) {
|
||||
*pre = *(pre-1);
|
||||
--pre;
|
||||
while(pre != first && compare(value, *(pre-1))) {
|
||||
*pre = *(pre-1);
|
||||
--pre;
|
||||
}
|
||||
|
||||
*pre = value;
|
||||
++it;
|
||||
}
|
||||
|
||||
*pre = value;
|
||||
++it;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -24,3 +24,10 @@ TEST(Algorithm, InsertionSort) {
|
||||
ASSERT_LT(arr[i], arr[i+1]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Algorithm, InsertionSortEmptyContainer) {
|
||||
std::vector<int> vec{};
|
||||
entt::insertion_sort sort;
|
||||
// this should crash with asan enabled if we break the constraint
|
||||
sort(vec.begin(), vec.end());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user