commit:     c720262bfd9a31512b03f2e3129839886d9d27c6
Author:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
AuthorDate: Fri Jul 12 17:59:20 2019 +0000
Commit:     Fabian Groffen <grobian <AT> gentoo <DOT> org>
CommitDate: Fri Jul 12 17:59:20 2019 +0000
URL:        https://gitweb.gentoo.org/proj/portage-utils.git/commit/?id=c720262b

libq/tree: avoid calling qsort with empty set

Encountering an empty directory in tree_next_pkg_int will not populate
cat_ctx->pkg_ctxs, so avoid calling qsort with it.  While at it, avoid
calling it for a single entry too.

Bug: https://bugs.gentoo.org/689290#c7
Signed-off-by: Fabian Groffen <grobian <AT> gentoo.org>

 libq/tree.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libq/tree.c b/libq/tree.c
index 9dfbe7d..6bb6a89 100644
--- a/libq/tree.c
+++ b/libq/tree.c
@@ -341,7 +341,6 @@ tree_next_pkg_int(tree_cat_ctx *cat_ctx)
        if (cat_ctx->ctx->do_sort) {
                if (cat_ctx->pkg_ctxs == NULL) {
                        size_t pkg_size = 0;
-                       cat_ctx->pkg_ctxs = NULL;
                        cat_ctx->pkg_cnt = 0;
                        cat_ctx->pkg_cur = 0;
                        while ((de = readdir(cat_ctx->dir)) != NULL) {
@@ -359,7 +358,7 @@ tree_next_pkg_int(tree_cat_ctx *cat_ctx)
                                        cat_ctx->pkg_cnt--;
                        }
 
-                       if (cat_ctx->ctx->pkgsortfunc != NULL) {
+                       if (cat_ctx->ctx->pkgsortfunc != NULL && 
cat_ctx->pkg_cnt > 1) {
                                qsort(cat_ctx->pkg_ctxs, cat_ctx->pkg_cnt,
                                                sizeof(*cat_ctx->pkg_ctxs), 
cat_ctx->ctx->pkgsortfunc);
                        }

Reply via email to