From ea539017fbbc972a8239a7944eaa5ce4960b0903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Tue, 19 Oct 2021 17:11:22 +0200 Subject: [PATCH] libsemanage: do not sort empty records MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do not sort empty records to avoid calling qsort(3) with a NULL pointer. qsort(3) might be annotated with the function attribute nonnull and UBSan then complains: database_join.c:80:2: runtime error: null pointer passed as argument 1, which is declared to never be null Signed-off-by: Christian Göttsche Reference: https://github.com/SELinuxProject/selinux/commit/ea539017fbbc972a8239a7944eaa5ce4960b0903 Conflict: Modify the file path. --- libsemanage/src/database_join.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/database_join.c b/src/database_join.c index b9b35a61..a49a6226 100644 --- a/src/database_join.c +++ b/src/database_join.c @@ -77,10 +77,14 @@ static int dbase_join_cache(semanage_handle_t * handle, dbase_join_t * dbase) goto err; /* Sort for quicker merge later */ - qsort(records1, rcount1, sizeof(record1_t *), - (int (*)(const void *, const void *))rtable1->compare2_qsort); - qsort(records2, rcount2, sizeof(record2_t *), - (int (*)(const void *, const void *))rtable2->compare2_qsort); + if (rcount1 > 0) { + qsort(records1, rcount1, sizeof(record1_t *), + (int (*)(const void *, const void *))rtable1->compare2_qsort); + } + if (rcount2 > 0) { + qsort(records2, rcount2, sizeof(record2_t *), + (int (*)(const void *, const void *))rtable2->compare2_qsort); + } /* Now merge into this dbase */ while (i < rcount1 || j < rcount2) { -- 2.27.0