From e6a5cc213971361a4a15d958975a6b48682d992c Mon Sep 17 00:00:00 2001 From: Martin Wilck Date: Fri, 15 Nov 2024 23:25:15 +0100 Subject: [PATCH] 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 --- shared/hash.c | 3 +++ testsuite/test-hash.c | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) 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) { -- 2.47.2