1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-05-06 09:56:12 +02:00

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

@ -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;

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.
*