[llvm-branch-commits] [libcxx] 297c839 - [libc++] fix std::sort(T**, T**)

Louis Dionne via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Dec 4 13:10:22 PST 2020


Author: Brett Gutstein
Date: 2020-12-04T16:05:21-05:00
New Revision: 297c839e2d22f9bc37f5f8cca7eb5924b49716d2

URL: https://github.com/llvm/llvm-project/commit/297c839e2d22f9bc37f5f8cca7eb5924b49716d2
DIFF: https://github.com/llvm/llvm-project/commit/297c839e2d22f9bc37f5f8cca7eb5924b49716d2.diff

LOG: [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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/libcxx/include/algorithm b/libcxx/include/algorithm
index 970d36c16d1e..2a854e7da209 100644
--- a/libcxx/include/algorithm
+++ b/libcxx/include/algorithm
@@ -4162,7 +4162,7 @@ inline _LIBCPP_INLINE_VISIBILITY
 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>

diff  --git a/libcxx/test/std/algorithms/alg.sorting/alg.sort/sort/sort.pass.cpp b/libcxx/test/std/algorithms/alg.sorting/alg.sort/sort/sort.pass.cpp
index 1adba43b5ed3..3058b6420055 100644
--- a/libcxx/test/std/algorithms/alg.sorting/alg.sort/sort/sort.pass.cpp
+++ b/libcxx/test/std/algorithms/alg.sorting/alg.sort/sort/sort.pass.cpp
@@ -132,6 +132,21 @@ test_larger_sorts(int N)
     test_larger_sorts(N, N);
 }
 
+void
+test_pointer_sort()
+{
+    static const int array_size = 10;
+    const int v[array_size] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
+    const int *pv[array_size];
+    for (int i = 0; i < array_size; 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 +170,7 @@ int main(int, char**)
     test_larger_sorts(1000);
     test_larger_sorts(1009);
 
+    test_pointer_sort();
+
   return 0;
 }


        


More information about the llvm-branch-commits mailing list