From 9594126239e0d21b88c3b8c535b6635b4a8b8892 Mon Sep 17 00:00:00 2001 From: Boris Staletic Date: Fri, 29 Mar 2024 18:30:05 +0100 Subject: 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 Signed-off-by: Fabian Groffen --- libq/xarray.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'libq') diff --git a/libq/xarray.c b/libq/xarray.c index 49b478be..3251a124 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 - @@ -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) -- cgit v1.2.3-65-gdbad