From: Emil Velikov Date: Wed, 9 Oct 2024 15:26:25 +0000 (+0100) Subject: Avoid adding zero to a NULL pointer X-Git-Tag: v34~235 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=944d970f0fa9e024810fc395f45a33c8e02dd6e6;p=thirdparty%2Fkmod.git Avoid adding zero to a NULL pointer 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 Link: https://github.com/kmod-project/kmod/pull/180 Signed-off-by: Lucas De Marchi --- diff --git a/shared/array.c b/shared/array.c index 38c04b50..7bedf45b 100644 --- a/shared/array.c +++ b/shared/array.c @@ -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); } diff --git a/shared/hash.c b/shared/hash.c index df35f453..38b7bb30 100644 --- a/shared/hash.c +++ b/shared/hash.c @@ -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; diff --git a/tools/depmod.c b/tools/depmod.c index a28666c7..a3f9801f 100644 --- a/tools/depmod.c +++ b/tools/depmod.c @@ -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++) {