summaryrefslogtreecommitdiff
path: root/hashmap.c
diff options
context:
space:
mode:
authorElijah Newren <newren@gmail.com>2020-11-02 18:55:03 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-11-02 20:15:50 (GMT)
commitb7879b0ba6ee1306a42227f7fd7f4e5f50409184 (patch)
tree32f328d974b2a59154e062edacda2158935d1fd7 /hashmap.c
parent97a39a4a930ebec9162f90ebd0412aed47d413d0 (diff)
downloadgit-b7879b0ba6ee1306a42227f7fd7f4e5f50409184.zip
git-b7879b0ba6ee1306a42227f7fd7f4e5f50409184.tar.gz
git-b7879b0ba6ee1306a42227f7fd7f4e5f50409184.tar.bz2
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>
Diffstat (limited to 'hashmap.c')
-rw-r--r--hashmap.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/hashmap.c b/hashmap.c
index e44d8a3..bb7c997 100644
--- a/hashmap.c
+++ b/hashmap.c
@@ -114,6 +114,7 @@ int hashmap_bucket(const struct hashmap *map, unsigned int hash)
static void rehash(struct hashmap *map, unsigned int newsize)
{
+ /* map->table MUST NOT be NULL when this function is called */
unsigned int i, oldsize = map->tablesize;
struct hashmap_entry **oldtable = map->table;
@@ -134,6 +135,7 @@ static void rehash(struct hashmap *map, unsigned int newsize)
static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map,
const struct hashmap_entry *key, const void *keydata)
{
+ /* map->table MUST NOT be NULL when this function is called */
struct hashmap_entry **e = &map->table[bucket(map, key)];
while (*e && !entry_equals(map, *e, key, keydata))
e = &(*e)->next;
@@ -196,6 +198,8 @@ struct hashmap_entry *hashmap_get(const struct hashmap *map,
const struct hashmap_entry *key,
const void *keydata)
{
+ if (!map->table)
+ return NULL;
return *find_entry_ptr(map, key, keydata);
}
@@ -211,8 +215,12 @@ struct hashmap_entry *hashmap_get_next(const struct hashmap *map,
void hashmap_add(struct hashmap *map, struct hashmap_entry *entry)
{
- unsigned int b = bucket(map, entry);
+ unsigned int b;
+
+ if (!map->table)
+ alloc_table(map, HASHMAP_INITIAL_SIZE);
+ b = bucket(map, entry);
/* add entry */
entry->next = map->table[b];
map->table[b] = entry;
@@ -230,7 +238,11 @@ struct hashmap_entry *hashmap_remove(struct hashmap *map,
const void *keydata)
{
struct hashmap_entry *old;
- struct hashmap_entry **e = find_entry_ptr(map, key, keydata);
+ struct hashmap_entry **e;
+
+ if (!map->table)
+ return NULL;
+ e = find_entry_ptr(map, key, keydata);
if (!*e)
return NULL;