diff mbox series

[2/3] libsemanage: do not sort empty records

Message ID 20211013125358.15534-2-cgzones@googlemail.com (mailing list archive)
State Changes Requested
Headers show
Series [1/3] libsepol: do not pass NULL to memcpy | expand

Commit Message

Christian Göttsche Oct. 13, 2021, 12:53 p.m. UTC
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 <cgzones@googlemail.com>
---
 libsemanage/src/database_join.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

Comments

Nicolas Iooss Oct. 16, 2021, 7:34 p.m. UTC | #1
On Wed, Oct 13, 2021 at 2:54 PM Christian Göttsche
<cgzones@googlemail.com> wrote:
>
> 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 <cgzones@googlemail.com>
> ---
>  libsemanage/src/database_join.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/libsemanage/src/database_join.c b/libsemanage/src/database_join.c
> index b9b35a61..b0e66e53 100644
> --- a/libsemanage/src/database_join.c
> +++ b/libsemanage/src/database_join.c
> @@ -77,10 +77,12 @@ 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.33.0
>

Hello,
This patch looks good, and the code would be more readable with braces
around the qsort calls (so it is clear with a fast glance over the
code that there is no issue of not using braces here). Could you add
braces like this?

if (rcount1 > 0) {
    qsort(records1, rcount1, sizeof(record1_t *),
        (int (*)(const void *, const void *))rtable1->compare2_qsort);
}

Thanks,
Nicolas
diff mbox series

Patch

diff --git a/libsemanage/src/database_join.c b/libsemanage/src/database_join.c
index b9b35a61..b0e66e53 100644
--- a/libsemanage/src/database_join.c
+++ b/libsemanage/src/database_join.c
@@ -77,10 +77,12 @@  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) {