mirror of
https://github.com/git/git
synced 2024-08-27 19:49:21 +00: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:
parent
97a39a4a93
commit
b7879b0ba6
16
hashmap.c
16
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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue