]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
stdlib: Avoid another self-comparison in qsort
authorFlorian Weimer <fweimer@redhat.com>
Tue, 21 Nov 2023 15:45:35 +0000 (16:45 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Tue, 21 Nov 2023 15:45:47 +0000 (16:45 +0100)
In the insertion phase, we could run off the start of the array if the
comparison function never runs zero.  In that case, it never finds the
initial element that terminates the iteration.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
stdlib/qsort.c

index ad110e8a892a66e1fc90f850b828e1a2d09e2ac5..6d0c4447ecb65fb1c8cf318b7b809bf147f5461b 100644 (file)
@@ -217,7 +217,7 @@ insertion_sort_qsort_partitions (void *const pbase, size_t total_elems,
   while ((run_ptr += size) <= end_ptr)
     {
       tmp_ptr = run_ptr - size;
-      while (cmp (run_ptr, tmp_ptr, arg) < 0)
+      while (run_ptr != tmp_ptr && cmp (run_ptr, tmp_ptr, arg) < 0)
         tmp_ptr -= size;
 
       tmp_ptr += size;