commit cb8cb1c5374b02e020cd9c396fc7c7c91cb3f39a
parent c06a945be3fc35232223b32c48484517a0a24752
Author: Jens Stutte <jstutte@mozilla.com>
Date: Wed, 3 Dec 2025 21:58:19 +0000
Bug 2003278 - Have a SortBoundsCheck option for nsTArray Sort and StableSort. r=xpcom-reviewers,layout-reviewers,emilio
Differential Revision: https://phabricator.services.mozilla.com/D274620
Diffstat:
2 files changed, 20 insertions(+), 6 deletions(-)
diff --git a/layout/painting/nsDisplayList.h b/layout/painting/nsDisplayList.h
@@ -3392,7 +3392,7 @@ class nsDisplayList {
for (nsDisplayItem* item : TakeItems()) {
items.AppendElement(Item(item));
}
- items.StableSort(aComparator);
+ items.template StableSort<SortBoundsCheck::Disable>(aComparator);
for (Item& item : items) {
AppendToTop(item);
diff --git a/xpcom/ds/nsTArray.h b/xpcom/ds/nsTArray.h
@@ -856,6 +856,8 @@ struct CompareWrapper<T, L, R, false> {
} // namespace detail
+enum class SortBoundsCheck { Enable, Disable };
+
//
// nsTArray_Impl contains most of the guts supporting nsTArray, FallibleTArray,
// AutoTArray.
@@ -2257,7 +2259,7 @@ class nsTArray_Impl
// nsTArray_RelocationStrategy.
//
// @param aComp The Comparator used to collate elements.
- template <class Comparator>
+ template <SortBoundsCheck Check = SortBoundsCheck::Enable, class Comparator>
void Sort(const Comparator& aComp) {
static_assert(std::is_move_assignable_v<value_type>);
static_assert(std::is_move_constructible_v<value_type>);
@@ -2266,13 +2268,20 @@ class nsTArray_Impl
auto compFn = [&comp](const auto& left, const auto& right) {
return comp.LessThan(left, right);
};
- std::sort(Elements(), Elements() + Length(), compFn);
+ if constexpr (Check == SortBoundsCheck::Enable) {
+ std::sort(begin(), end(), compFn);
+ } else {
+ std::sort(Elements(), Elements() + Length(), compFn);
+ }
::detail::AssertStrictWeakOrder(Elements(), Elements() + Length(), compFn);
}
// A variation on the Sort method defined above that assumes that
// 'operator<' is defined for 'value_type'.
- void Sort() { Sort(nsDefaultComparator<value_type, value_type>()); }
+ template <SortBoundsCheck Check = SortBoundsCheck::Enable>
+ void Sort() {
+ Sort(nsDefaultComparator<value_type, value_type>());
+ }
// This method sorts the elements of the array in a stable way (i.e. not
// changing the relative order of elements considered equal by the
@@ -2283,7 +2292,7 @@ class nsTArray_Impl
// nsTArray_RelocationStrategy.
//
// @param aComp The Comparator used to collate elements.
- template <class Comparator>
+ template <SortBoundsCheck Check = SortBoundsCheck::Enable, class Comparator>
void StableSort(const Comparator& aComp) {
static_assert(std::is_move_assignable_v<value_type>);
static_assert(std::is_move_constructible_v<value_type>);
@@ -2293,12 +2302,17 @@ class nsTArray_Impl
auto compFn = [&comp](const auto& lhs, const auto& rhs) {
return comp.LessThan(lhs, rhs);
};
- std::stable_sort(Elements(), Elements() + Length(), compFn);
+ if constexpr (Check == SortBoundsCheck::Enable) {
+ std::stable_sort(begin(), end(), compFn);
+ } else {
+ std::stable_sort(Elements(), Elements() + Length(), compFn);
+ }
::detail::AssertStrictWeakOrder(Elements(), Elements() + Length(), compFn);
}
// A variation on the StableSort method defined above that assumes that
// 'operator<' is defined for 'value_type'.
+ template <SortBoundsCheck Check = SortBoundsCheck::Enable>
void StableSort() {
StableSort(nsDefaultComparator<value_type, value_type>());
}