[libc-commits] [libc] [libc] Bound the worst-case stack usage in qsort(). (PR #110849)

Simon Tatham via libc-commits libc-commits at lists.llvm.org
Thu Oct 3 01:18:26 PDT 2024


================
@@ -59,17 +59,33 @@ static size_t partition(const Array &array) {
   }
 }
 
-LIBC_INLINE void quick_sort(const Array &array) {
-  const size_t array_size = array.size();
-  if (array_size <= 1)
-    return;
-  size_t split_index = partition(array);
-  if (array_size <= 2) {
-    // The partition operation sorts the two element array.
-    return;
+LIBC_INLINE void quick_sort(Array array) {
+  while (true) {
+    const size_t array_size = array.size();
+    if (array_size <= 1)
+      return;
+    size_t split_index = partition(array);
+    if (array_size <= 2) {
+      // The partition operation sorts the two element array.
+      return;
+    }
----------------
statham-arm wrote:

That construction was unchanged from the previous version of the code – all _I've_ done is to move it right by two spaces! But fine, I can fix it while I'm here.

https://github.com/llvm/llvm-project/pull/110849


More information about the libc-commits mailing list