From: Martin Wilck Date: Fri, 15 Nov 2024 22:25:15 +0000 (+0100) Subject: libkmod: hash_del: handle deleting a non-existing element gracefully X-Git-Tag: v34~45 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e6a5cc213971361a4a15d958975a6b48682d992c;p=thirdparty%2Fkmod.git libkmod: hash_del: handle deleting a non-existing element gracefully It can happen that bucket->entries is NULL, but bsearch is annotated such that it's second argument must be non-NULL. Fixes: 7db0865 ("Add simple hash implementation") Signed-off-by: Martin Wilck Reviewed-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/257 Signed-off-by: Lucas De Marchi --- diff --git a/shared/hash.c b/shared/hash.c index 158d85d9..f9a0c00c 100644 --- a/shared/hash.c +++ b/shared/hash.c @@ -249,6 +249,9 @@ int hash_del(struct hash *hash, const char *key) .value = NULL, }; + if (bucket->entries == NULL) + return -ENOENT; + entry = bsearch(&se, bucket->entries, bucket->used, sizeof(struct hash_entry), hash_entry_cmp); if (entry == NULL) diff --git a/testsuite/test-hash.c b/testsuite/test-hash.c index e9041117..35cf06ed 100644 --- a/testsuite/test-hash.c +++ b/testsuite/test-hash.c @@ -200,8 +200,7 @@ static int test_hash_del_nonexistent(const struct test *t) return 0; } DEFINE_TEST(test_hash_del_nonexistent, - .description = "test deleting an element that doesn't exist", - .expected_fail = true); + .description = "test deleting an element that doesn't exist"); static int test_hash_free(const struct test *t) {