[1/2] stdlib: Fix heapsort for cases with exactly two elements

Message ID 20240116021657.2553198-2-visitorckw@gmail.com
State Committed
Commit 74d2731a5fb2676b64092bc25e7f193db1b17b2b
Headers
Series stdlib: Fix and verify heapsort for two-element cases |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Testing passed

Commit Message

Kuan-Wei Chiu Jan. 16, 2024, 2:16 a.m. UTC
  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>
---
 stdlib/qsort.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Adhemerval Zanella Netto Jan. 16, 2024, 10:46 a.m. UTC | #1
On 15/01/24 23:16, Kuan-Wei Chiu wrote:
> 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>

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  stdlib/qsort.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/stdlib/qsort.c b/stdlib/qsort.c
> index b29882388e..45af8da80c 100644
> --- a/stdlib/qsort.c
> +++ b/stdlib/qsort.c
> @@ -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);
  

Patch

diff --git a/stdlib/qsort.c b/stdlib/qsort.c
index b29882388e..45af8da80c 100644
--- a/stdlib/qsort.c
+++ b/stdlib/qsort.c
@@ -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);