hashmap: allow re-use after hashmap_free()

Previously, once map->table had been freed, any calls to hashmap_put(),
hashmap_get(), or hashmap_remove() would cause a NULL pointer
dereference (since hashmap_free_() also zeros the memory; without that
zeroing, calling these functions would cause a use-after-free problem).

Modify these functions to check for a NULL table and automatically
allocate as needed.

Also add a HASHMAP_INIT(fn, data) macro for initializing hashmaps on the
stack without calling hashmap_init().

Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Elijah Newren
2020-11-02 18:55:03 +00:00
committed by Junio C Hamano
parent 97a39a4a93
commit b7879b0ba6
2 changed files with 17 additions and 2 deletions

View File

@ -210,6 +210,9 @@ struct hashmap {
/* hashmap functions */
#define HASHMAP_INIT(fn, data) { .cmpfn = fn, .cmpfn_data = data, \
.do_count_items = 1 }
/*
* Initializes a hashmap structure.
*