diff options
author | Leandro Pereira <leandro@hardinfo.org> | 2012-10-12 12:28:56 -0300 |
---|---|---|
committer | Lucas De Marchi <lucas.de.marchi@gmail.com> | 2012-10-12 12:34:27 -0300 |
commit | 1faec2c1344fd2725152a6944dc5ac2525999ec6 (patch) | |
tree | c9dcac4eccaefa140399950d407ddee1ff8a62b6 | |
parent | 66f3228d17d66d7e2dd484427259290fbc82b2f0 (diff) | |
download | kmod-1faec2c1344fd2725152a6944dc5ac2525999ec6.tar.gz |
libkmod-hash: Plug possible memory leak when free_value is defined
Although the hash table implementation allows passing a callback function
to free a value when it is removed from the hash table, hash_del() wasn't
freeing it if it was provided. Now it does.
As a bonus, it now checks if the callback is set in hash_add() as well.
-rw-r--r-- | libkmod/libkmod-hash.c | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/libkmod/libkmod-hash.c b/libkmod/libkmod-hash.c index 1d02da5..dc9910b 100644 --- a/libkmod/libkmod-hash.c +++ b/libkmod/libkmod-hash.c @@ -164,7 +164,8 @@ int hash_add(struct hash *hash, const char *key, const void *value) for (; entry < entry_end; entry++) { int c = strcmp(key, entry->key); if (c == 0) { - hash->free_value((void *)entry->value); + if (hash->free_value) + hash->free_value((void *)entry->value); entry->value = value; return 0; } else if (c < 0) { @@ -263,6 +264,9 @@ int hash_del(struct hash *hash, const char *key) if (entry == NULL) return -ENOENT; + if (hash->free_value) + hash->free_value((void *)entry->value); + entry_end = bucket->entries + bucket->used; memmove(entry, entry + 1, (entry_end - entry) * sizeof(struct hash_entry)); |