From 8ee8ab30999b7850b7db0d74b9e05211a6949c38 Mon Sep 17 00:00:00 2001 From: Wenyong Huang Date: Tue, 28 Mar 2023 14:50:31 +0800 Subject: [PATCH] Limit the minimal size of bh_hashmap (#2073) Limit the minimal size of bh_hashmap to avoid creating hashmap with size 0, and `divide by zero` when calling bh_hash_map_find. Reported by #2008. --- core/shared/utils/bh_hashmap.c | 3 +++ core/shared/utils/bh_hashmap.h | 3 +++ 2 files changed, 6 insertions(+) diff --git a/core/shared/utils/bh_hashmap.c b/core/shared/utils/bh_hashmap.c index 70119c77..3502239a 100644 --- a/core/shared/utils/bh_hashmap.c +++ b/core/shared/utils/bh_hashmap.c @@ -33,6 +33,9 @@ bh_hash_map_create(uint32 size, bool use_lock, HashFunc hash_func, HashMap *map; uint64 total_size; + if (size < HASH_MAP_MIN_SIZE) + size = HASH_MAP_MIN_SIZE; + if (size > HASH_MAP_MAX_SIZE) { LOG_ERROR("HashMap create failed: size is too large.\n"); return NULL; diff --git a/core/shared/utils/bh_hashmap.h b/core/shared/utils/bh_hashmap.h index ef06960f..38aa2c66 100644 --- a/core/shared/utils/bh_hashmap.h +++ b/core/shared/utils/bh_hashmap.h @@ -12,6 +12,9 @@ extern "C" { #endif +/* Minimum initial size of hash map */ +#define HASH_MAP_MIN_SIZE 4 + /* Maximum initial size of hash map */ #define HASH_MAP_MAX_SIZE 65536