]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Avoid adding zero to a NULL pointer
authorEmil Velikov <emil.l.velikov@gmail.com>
Wed, 9 Oct 2024 15:26:25 +0000 (16:26 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Thu, 17 Oct 2024 13:28:08 +0000 (08:28 -0500)
Adding zero to a NULL pointer is undefined behaviour, which is getting
clarified with C23 (or just after) to match our current usage.

With clang 18, this triggers the undefined behaviour sanitizer so add a
check to stay compliant.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/180
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
shared/array.c
shared/hash.c
tools/depmod.c

index 38c04b50490a4613d1e7ff58c91d69992ae35f00..7bedf45bccdc87b17ca85e5653f10c50cf2ccca5 100644 (file)
@@ -69,10 +69,14 @@ int array_append(struct array *array, const void *element)
 int array_append_unique(struct array *array, const void *element)
 {
        void **itr = array->array;
-       void **itr_end = itr + array->count;
-       for (; itr < itr_end; itr++)
-               if (*itr == element)
-                       return -EEXIST;
+
+       if (array->count != 0) {
+               void **itr_end = itr + array->count;
+
+               for (; itr < itr_end; itr++)
+                       if (*itr == element)
+                               return -EEXIST;
+       }
        return array_append(array, element);
 }
 
index df35f45388f86cbedc360e85fddfa22772896425..38b7bb30125c034ed300dbf040d8afcd8dfabb8d 100644 (file)
@@ -58,7 +58,7 @@ void hash_free(struct hash *hash)
        bucket = hash->buckets;
        bucket_end = bucket + hash->n_buckets;
        for (; bucket < bucket_end; bucket++) {
-               if (hash->free_value) {
+               if (hash->free_value && bucket->used != 0) {
                        struct hash_entry *entry, *entry_end;
                        entry = bucket->entries;
                        entry_end = entry + bucket->used;
index a28666c7d9223fd4f0ce2d3369f1b73cf047fd62..a3f9801ff7a2225c1cc6c91bf51ce98e30246f2e 100644 (file)
@@ -1944,6 +1944,9 @@ static int depmod_calculate_dependencies(struct depmod *depmod)
                sorted[n_sorted] = src_idx;
                n_sorted++;
 
+               if (src->deps.count == 0)
+                       continue;
+
                itr_dst = (const struct mod **)src->deps.array;
                itr_dst_end = itr_dst + src->deps.count;
                for (; itr_dst < itr_dst_end; itr_dst++) {