diff options
author | Dmitry Antipov <dmantipov@yandex.ru> | 2023-05-19 10:41:08 +0300 |
---|---|---|
committer | Lucas De Marchi <lucas.de.marchi@gmail.com> | 2023-05-30 22:36:43 -0700 |
commit | 9c262fdb1c798fd87d91e8c669acbec4d632024b (patch) | |
tree | 8de70b43fb1e292194c96aa9eaf2733c56055e6f | |
parent | badacf76e46b3602bc0e99ffc677ccbe53691f62 (diff) | |
download | kmod-9c262fdb1c798fd87d91e8c669acbec4d632024b.tar.gz kmod-9c262fdb1c798fd87d91e8c669acbec4d632024b.tar.bz2 kmod-9c262fdb1c798fd87d91e8c669acbec4d632024b.zip |
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 <christophe.leroy@csgroup.eu>
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
[ reshuffle the code to use return-early style ]
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
-rw-r--r-- | shared/hash.c | 13 |
1 files changed, 8 insertions, 5 deletions
diff --git a/shared/hash.c b/shared/hash.c index 7fe3f80..a87bc50 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) |