From: Florian Weimer Date: Tue, 21 Nov 2023 15:45:35 +0000 (+0100) Subject: stdlib: Avoid another self-comparison in qsort X-Git-Tag: glibc-2.39~265 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e4d8117b82065dc72e8df80097360e7c05a349b9;p=thirdparty%2Fglibc.git stdlib: Avoid another self-comparison in qsort 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 --- diff --git a/stdlib/qsort.c b/stdlib/qsort.c index ad110e8a892..6d0c4447ecb 100644 --- a/stdlib/qsort.c +++ b/stdlib/qsort.c @@ -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;