From: Dmitry Antipov Date: Fri, 19 May 2023 07:41:08 +0000 (+0300) Subject: shared: avoid passing {NULL, 0} array to bsearch() X-Git-Tag: v31~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=9c262fdb1c798fd87d91e8c669acbec4d632024b;p=thirdparty%2Fkmod.git shared: avoid passing {NULL, 0} array to bsearch() Fix the following warning reported by UBSan (as of gcc-13.1.1): shared/hash.c:244:35: runtime error: null pointer passed as argument 2, which is declared to never be null Reviewed-by: Christophe Leroy Signed-off-by: Dmitry Antipov [ reshuffle the code to use return-early style ] Signed-off-by: Lucas De Marchi --- diff --git a/shared/hash.c b/shared/hash.c index 7fe3f807..a87bc507 100644 --- a/shared/hash.c +++ b/shared/hash.c @@ -241,12 +241,15 @@ void *hash_find(const struct hash *hash, const char *key) .key = key, .value = NULL }; - const struct hash_entry *entry = bsearch( - &se, bucket->entries, bucket->used, - sizeof(struct hash_entry), hash_entry_cmp); - if (entry == NULL) + const struct hash_entry *entry; + + if (!bucket->entries) return NULL; - return (void *)entry->value; + + entry = bsearch(&se, bucket->entries, bucket->used, + sizeof(struct hash_entry), hash_entry_cmp); + + return entry ? (void *)entry->value : NULL; } int hash_del(struct hash *hash, const char *key)