From b7879b0ba6ee1306a42227f7fd7f4e5f50409184 Mon Sep 17 00:00:00 2001 From: Elijah Newren Date: Mon, 2 Nov 2020 18:55:03 +0000 Subject: 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 Signed-off-by: Junio C Hamano --- hashmap.h | 3 +++ 1 file changed, 3 insertions(+) (limited to 'hashmap.h') diff --git a/hashmap.h b/hashmap.h index 904f61d6e1..3b0f2bcade 100644 --- a/hashmap.h +++ b/hashmap.h @@ -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. * -- cgit v1.2.3