fix(hash_map): fixed leak on value update

This commit is contained in:
william.valenduc 2026-01-19 15:28:35 +00:00
parent 3cf1960a00
commit 2ebf56dde7
4 changed files with 251 additions and 13 deletions

View file

@ -55,7 +55,7 @@ struct hash_map *hash_map_init(size_t size)
bool hash_map_insert(struct hash_map *hash_map, const char *key, void *value,
bool *updated)
{
if (hash_map == NULL || hash_map->size == 0 || key == NULL)
if (hash_map == NULL || hash_map->size == 0 || key == NULL || value == NULL)
return false;
size_t h = hash(key);
@ -71,6 +71,7 @@ bool hash_map_insert(struct hash_map *hash_map, const char *key, void *value,
if (strcmp(iter->key, key) == 0)
{
// update
free(iter->value);
iter->value = value;
if (updated)
*updated = true;
@ -96,16 +97,16 @@ bool hash_map_insert(struct hash_map *hash_map, const char *key, void *value,
return true;
}
void hash_map_free(struct hash_map *hash_map)
void hash_map_free(struct hash_map **hash_map)
{
struct pair_list *l;
struct pair_list *prev;
if (hash_map)
if (hash_map != NULL && *hash_map != NULL)
{
for (size_t i = 0; i < hash_map->size; i++)
for (size_t i = 0; i < (*hash_map)->size; i++)
{
l = hash_map->data[i];
l = (*hash_map)->data[i];
while (l != NULL)
{
prev = l;
@ -113,8 +114,8 @@ void hash_map_free(struct hash_map *hash_map)
destroy_pair_list(&prev);
}
}
free(hash_map->data);
free(hash_map);
free((*hash_map)->data);
free(*hash_map);
}
}

View file

@ -19,10 +19,23 @@ struct hash_map
struct hash_map *hash_map_init(size_t size);
/**
* @brief Inserts a key-value pair into the hash map. Key and value are expected
* to be on the heap and will be managed by the hash map. It means they are
* consumed by this function. If the key already exists, its value is updated
* and the key is not consumed so it must be freed by the caller.
*
* @param hash_map The hash map.
* @param key The key to insert.
* @param value The value to insert.
* @param updated If not NULL, set to true if the key was already present and
* updated, false if the key was newly inserted.
* @return true on success, false on failure.
*/
bool hash_map_insert(struct hash_map *hash_map, const char *key, void *value,
bool *updated);
void hash_map_free(struct hash_map *hash_map);
void hash_map_free(struct hash_map **hash_map);
void hash_map_foreach(struct hash_map *hash_map,
void (*fn)(const char *, const void *));

View file

@ -27,14 +27,21 @@ char *get_var_or_env(const struct hash_map *vars, const char *key)
return value;
}
bool set_var(struct hash_map *vars, const char *key, const char *value)
bool set_var(struct hash_map *vars, const char *key, const char *value,
bool *updated)
{
if (key == NULL || value == NULL)
return false;
return hash_map_insert(vars, key, (void *)value, NULL);
return hash_map_insert(vars, key, (void *)value, updated);
}
bool set_var_copy(struct hash_map *vars, const char *key, const char *value)
{
return set_var(vars, strdup(key), strdup(value));
char *key_copy = strdup(key);
char *value_copy = strdup(value);
bool updated;
bool res = set_var(vars, key_copy, value_copy, &updated);
if (updated || !res)
free(key_copy);
if (!res)
free(value_copy);
return res;
}