]> sourceware.org Git - glibc.git/commitdiff
stdlib: Fix heapsort for cases with exactly two elements
authorKuan-Wei Chiu <visitorckw@gmail.com>
Tue, 16 Jan 2024 02:16:56 +0000 (10:16 +0800)
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>
Tue, 16 Jan 2024 14:00:51 +0000 (11:00 -0300)
When malloc fails to allocate a buffer and falls back to heapsort, the
current heapsort implementation does not perform sorting when there are
exactly two elements. Heapsort is now skipped only when there is
exactly one element.

Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
stdlib/qsort.c

index b29882388e26cc6f67597418875f98b0f8bc6826..45af8da80c3226a19c414a3c99b0c16617e27293 100644 (file)
@@ -162,7 +162,7 @@ get_swap_type (void *const pbase, size_t size)
 static void
 heapsort_r (void *base, size_t n, size_t size, __compar_d_fn_t cmp, void *arg)
 {
-  if (n <= 1)
+  if (n == 0)
     return;
 
   enum swap_type_t swap_type = get_swap_type (base, size);
This page took 0.044296 seconds and 5 git commands to generate.