Skip to content

Commit 297c839

Browse files
brettferdosildionne
authored andcommitted
[libc++] fix std::sort(T**, T**)
previously, invocations of std::sort(T**, T**) casted the arguments to (size_t *). this breaks sorting on systems for which pointers don't fit in a size_t. change the cast to (uintptr_t *) and add a test. Differential Revision: https://reviews.llvm.org/D92190
1 parent 650e04e commit 297c839

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

libcxx/include/algorithm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4162,7 +4162,7 @@ inline _LIBCPP_INLINE_VISIBILITY
41624162
void
41634163
sort(_Tp** __first, _Tp** __last)
41644164
{
4165-
_VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
4165+
_VSTD::sort((uintptr_t*)__first, (uintptr_t*)__last, __less<uintptr_t>());
41664166
}
41674167

41684168
template <class _Tp>

libcxx/test/std/algorithms/alg.sorting/alg.sort/sort/sort.pass.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,21 @@ test_larger_sorts(int N)
132132
test_larger_sorts(N, N);
133133
}
134134

135+
void
136+
test_pointer_sort()
137+
{
138+
static const int array_size = 10;
139+
const int v[array_size] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
140+
const int *pv[array_size];
141+
for (int i = 0; i < array_size; i++) {
142+
pv[i] = &v[array_size - 1 - i];
143+
}
144+
std::sort(pv, pv + array_size);
145+
assert(*pv[0] == v[0]);
146+
assert(*pv[1] == v[1]);
147+
assert(*pv[array_size - 1] == v[array_size - 1]);
148+
}
149+
135150
int main(int, char**)
136151
{
137152
// test null range
@@ -155,5 +170,7 @@ int main(int, char**)
155170
test_larger_sorts(1000);
156171
test_larger_sorts(1009);
157172

173+
test_pointer_sort();
174+
158175
return 0;
159176
}

0 commit comments

Comments
 (0)