diff mbox series

[45/47] libsemanage: skip sort of empty arrays

Message ID 20241111141706.38039-45-cgoettsche@seltendoof.de (mailing list archive)
State New
Delegated to: Petr Lautrbach
Headers show
Series [01/47] libsemanage: white space cleanup | expand

Commit Message

Christian Göttsche Nov. 11, 2024, 2:17 p.m. UTC
From: Christian Göttsche <cgzones@googlemail.com>

UBSAN triggers if the first argument of qsort(3) is NULL, even if the
associated passed size is 0 and thus no actual dereference will happen.

Sort only arrays with at least two elements.

Signed-off-by: Christian Göttsche <cgzones@googlemail.com>
---
 libsemanage/src/database_join.c     | 4 ++--
 libsemanage/src/ibendports_local.c  | 3 ++-
 libsemanage/src/ibpkeys_local.c     | 3 ++-
 libsemanage/src/policy_components.c | 2 +-
 libsemanage/src/ports_local.c       | 4 +++-
 libsemanage/src/utilities.c         | 2 +-
 6 files changed, 11 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/libsemanage/src/database_join.c b/libsemanage/src/database_join.c
index abe6c43c..afb67618 100644
--- a/libsemanage/src/database_join.c
+++ b/libsemanage/src/database_join.c
@@ -77,10 +77,10 @@  static int dbase_join_cache(semanage_handle_t * handle, dbase_join_t * dbase)
 		goto err;
 
 	/* Sort for quicker merge later */
-	if (rcount1 > 0) {
+	if (rcount1 > 1) {
 		qsort(records1, rcount1, sizeof(record1_t *), rtable1->compare2_qsort);
 	}
-	if (rcount2 > 0) {
+	if (rcount2 > 1) {
 		qsort(records2, rcount2, sizeof(record2_t *), rtable2->compare2_qsort);
 	}
 
diff --git a/libsemanage/src/ibendports_local.c b/libsemanage/src/ibendports_local.c
index 0b8c14d2..acf1a37c 100644
--- a/libsemanage/src/ibendports_local.c
+++ b/libsemanage/src/ibendports_local.c
@@ -89,7 +89,8 @@  int semanage_ibendport_validate_local(semanage_handle_t *handle)
 	if (semanage_ibendport_list_local(handle, &ibendports, &nibendports) < 0)
 		goto err;
 
-	qsort(ibendports, nibendports, sizeof(semanage_ibendport_t *), semanage_ibendport_compare2_qsort);
+	if (nibendports > 1)
+		qsort(ibendports, nibendports, sizeof(semanage_ibendport_t *), semanage_ibendport_compare2_qsort);
 
 	/* Test each ibendport */
 	while (i < nibendports) {
diff --git a/libsemanage/src/ibpkeys_local.c b/libsemanage/src/ibpkeys_local.c
index 6848bd0d..eea30c2f 100644
--- a/libsemanage/src/ibpkeys_local.c
+++ b/libsemanage/src/ibpkeys_local.c
@@ -91,7 +91,8 @@  int semanage_ibpkey_validate_local(semanage_handle_t *handle)
 	if (semanage_ibpkey_list_local(handle, &ibpkeys, &nibpkeys) < 0)
 		goto err;
 
-	qsort(ibpkeys, nibpkeys, sizeof(semanage_ibpkey_t *), semanage_ibpkey_compare2_qsort);
+	if (nibpkeys > 1)
+		qsort(ibpkeys, nibpkeys, sizeof(semanage_ibpkey_t *), semanage_ibpkey_compare2_qsort);
 
 	/* Test each ibpkey for overlap */
 	while (i < nibpkeys) {
diff --git a/libsemanage/src/policy_components.c b/libsemanage/src/policy_components.c
index 6557bb18..20f743dc 100644
--- a/libsemanage/src/policy_components.c
+++ b/libsemanage/src/policy_components.c
@@ -168,7 +168,7 @@  int semanage_base_merge_components(semanage_handle_t * handle)
 			goto err;
 
 		/* Sort records on MODE_SORT */
-		if (mode & MODE_SORT) {
+		if ((mode & MODE_SORT) && nrecords > 1) {
 			qsort(records, nrecords, sizeof(record_t *), rtable->compare2_qsort);
 		}
 
diff --git a/libsemanage/src/ports_local.c b/libsemanage/src/ports_local.c
index 7d4d0768..5711bdd8 100644
--- a/libsemanage/src/ports_local.c
+++ b/libsemanage/src/ports_local.c
@@ -82,7 +82,9 @@  int semanage_port_validate_local(semanage_handle_t * handle)
 	/* List and sort the ports */
 	if (semanage_port_list_local(handle, &ports, &nports) < 0)
 		goto err;
-	qsort(ports, nports, sizeof(semanage_port_t *), semanage_port_compare2_qsort);
+
+	if (nports > 1)
+		qsort(ports, nports, sizeof(semanage_port_t *), semanage_port_compare2_qsort);
 
 	/* Test each port for overlap */
 	while (i < nports) {
diff --git a/libsemanage/src/utilities.c b/libsemanage/src/utilities.c
index 9df7b9c1..70b5b677 100644
--- a/libsemanage/src/utilities.c
+++ b/libsemanage/src/utilities.c
@@ -167,7 +167,7 @@  int semanage_list_sort(semanage_list_t ** l)
 	size_t count = 0;
 	size_t i = 0;
 
-	if (!l)
+	if (!l || !(*l)->next)
 		return 0;
 
 	for (temp = *l; temp; temp = temp->next)