Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance the robustness of the bh_hashmap code #2008

Closed
ApsarasX opened this issue Mar 6, 2023 · 1 comment
Closed

Enhance the robustness of the bh_hashmap code #2008

ApsarasX opened this issue Mar 6, 2023 · 1 comment

Comments

@ApsarasX
Copy link

ApsarasX commented Mar 6, 2023

void *
bh_hash_map_find(HashMap *map, void *key)
{
uint32 index;
HashMapElem *elem;
void *value;
if (!map || !key) {
LOG_ERROR("HashMap find elem failed: map or key is NULL.\n");
return NULL;
}
if (map->lock) {
os_mutex_lock(map->lock);
}
index = map->hash_func(key) % map->size;
elem = map->elements[index];
while (elem) {
if (map->key_equal_func(elem->key, key)) {
value = elem->value;
if (map->lock) {
os_mutex_unlock(map->lock);
}
return value;
}
elem = elem->next;
}
if (map->lock) {
os_mutex_unlock(map->lock);
}
return NULL;
}

In the above code, map ->size may be equal to zero, so there may be an exception of divide by zero.

Although all the places where the bh_hash_map_create function is used have a non-zero size, I think it is still necessary to detect the size inside the hashmap in order to enhance the robustness of the code.

@wenyongh
Copy link
Contributor

Hi, thanks for report the issue! I submitted PR #2073 to enhance the code.

wenyongh added a commit that referenced this issue Mar 28, 2023
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.
victoryang00 pushed a commit to victoryang00/wamr-aot-gc-checkpoint-restore that referenced this issue May 27, 2024
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 bytecodealliance#2008.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants