commit:     9594126239e0d21b88c3b8c535b6635b4a8b8892
Author:     Boris Staletic <boris.staletic <AT> protonmail <DOT> com>
AuthorDate: Fri Mar 29 17:30:05 2024 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Mon Apr  8 19:26:59 2024 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=95941262

libq/xarray: Handle NULL arrays in xarraysort()

Some invocations of `q` may try to call `xarraysort(NULL, 0)`.
One example is `qlop -a foo` where `foo` was never unmerged.

Instead of requiring every call of `xarraysort()` to take care of `NULL`
arguments, `xarraysort()` now exits early if `arr->eles == NULL`.

Closes: https://github.com/gentoo/portage-utils/pull/28
Signed-off-by: Boris Staletic <boris.staletic <AT> protonmail.com>
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 libq/xarray.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/libq/xarray.c b/libq/xarray.c
index 49b478b..3251a12 100644
--- a/libq/xarray.c
+++ b/libq/xarray.c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2003-2019 Gentoo Foundation
+ * Copyright 2003-2024 Gentoo Foundation
  * Distributed under the terms of the GNU General Public License v2
  *
  * Copyright 2003-2007 Ned Ludd        - <so...@gentoo.org>
@@ -46,7 +46,8 @@ void *xarraypush(array_t *arr, const void *ele, size_t 
ele_len)
 
 void xarraysort(array_t *arr, int (*compar)(const void *, const void *))
 {
-       qsort(arr->eles, arr->num, sizeof(void *), compar);
+       if (arr->num > 1)
+               qsort(arr->eles, arr->num, sizeof(void *), compar);
 }
 
 void xarraydelete_ptr(array_t *arr, size_t elem)

Reply via email to