[libcxx-commits] [PATCH] D92190: [libc++] fix std::sort(T**, T**)

Brett Gutstein via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Thu Nov 26 09:35:02 PST 2020


bfg created this revision.
bfg added reviewers: libc++, arichardson, ldionne.
Herald added subscribers: libcxx-commits, mgrang.
Herald added a project: libc++.
Herald added 1 blocking reviewer(s): libc++.
bfg requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D92190

Files:
  libcxx/include/algorithm
  libcxx/test/std/algorithms/alg.sorting/alg.sort/sort/sort.pass.cpp


Index: libcxx/test/std/algorithms/alg.sorting/alg.sort/sort/sort.pass.cpp
===================================================================
--- libcxx/test/std/algorithms/alg.sorting/alg.sort/sort/sort.pass.cpp
+++ libcxx/test/std/algorithms/alg.sorting/alg.sort/sort/sort.pass.cpp
@@ -132,6 +132,22 @@
     test_larger_sorts(N, N);
 }
 
+void
+test_pointer_sort()
+{
+    int array_size = 10;
+    int v[array_size];
+    int *pv[array_size];
+    for (int i = 0; i < array_size; i++) {
+      v[i] = i;
+      pv[i] = &v[array_size - 1 - i];
+    }
+    std::sort(pv, pv + array_size);
+    assert(*pv[0] == v[0]);
+    assert(*pv[1] == v[1]);
+    assert(*pv[array_size - 1] == v[array_size - 1]);
+}
+
 int main(int, char**)
 {
     // test null range
@@ -155,5 +171,7 @@
     test_larger_sorts(1000);
     test_larger_sorts(1009);
 
+    test_pointer_sort();
+
   return 0;
 }
Index: libcxx/include/algorithm
===================================================================
--- libcxx/include/algorithm
+++ libcxx/include/algorithm
@@ -4162,7 +4162,7 @@
 void
 sort(_Tp** __first, _Tp** __last)
 {
-    _VSTD::sort((size_t*)__first, (size_t*)__last, __less<size_t>());
+    _VSTD::sort((uintptr_t*)__first, (uintptr_t*)__last, __less<uintptr_t>());
 }
 
 template <class _Tp>


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D92190.307900.patch
Type: text/x-patch
Size: 1286 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20201126/2e60f509/attachment.bin>


More information about the libcxx-commits mailing list