From 12986286deae1dfbf4c400bb9f0d74061414a1be Mon Sep 17 00:00:00 2001 From: Emil Hammarstrom Date: Tue, 23 Sep 2025 16:15:54 +0200 Subject: [PATCH] lib: hash: Ensure OA/LP hmap inserts don't return tombstones According to the hashmap interface specification sys_hashmap_remove will "Erase the entry associated with key `key`, if one exists" If a rehash is performed OA/LP will return 1 and no old_value for sys_hashmap_insert. If a rehash is NOT performed OA/LP will return 0 and a stale old_value for sys_hashmap_insert even though the user requested the entry for that key to be removed. This patch makes OA/LP not return the value of tombstoned entries. Signed-off-by: Emil Hammarstrom --- lib/hash/hash_map_oa_lp.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/hash/hash_map_oa_lp.c b/lib/hash/hash_map_oa_lp.c index e40112dfad3..e6e47ac65cf 100644 --- a/lib/hash/hash_map_oa_lp.c +++ b/lib/hash/hash_map_oa_lp.c @@ -84,23 +84,22 @@ static int sys_hashmap_oa_lp_insert_no_rehash(struct sys_hashmap *map, uint64_t __ASSERT_NO_MSG(entry != NULL); switch (entry->state) { - case UNUSED: - ++data->size; - ret = 1; - break; case TOMBSTONE: --data->n_tombstones; ++data->size; - ret = 0; + ret = 1; break; case USED: - default: + if (old_value != NULL) { + *old_value = entry->value; + } ret = 0; break; - } - - if (old_value != NULL) { - *old_value = entry->value; + case UNUSED: + default: + ++data->size; + ret = 1; + break; } entry->state = USED;