]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
shared: avoid passing {NULL, 0} array to bsearch()
authorDmitry Antipov <dmantipov@yandex.ru>
Fri, 19 May 2023 07:41:08 +0000 (10:41 +0300)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Wed, 31 May 2023 05:36:43 +0000 (22:36 -0700)
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>
shared/hash.c

index 7fe3f807711add1fe39c6c4c2a07c07c5eb53c5a..a87bc5074ee611e790ca493eb7c1b599536ec672 100644 (file)
@@ -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)