libkmod-hash: Plug possible memory leak when free_value is defined
authorLeandro Pereira <leandro@hardinfo.org>
Fri, 12 Oct 2012 15:28:56 +0000 (12:28 -0300)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 12 Oct 2012 15:34:27 +0000 (12:34 -0300)
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.

libkmod/libkmod-hash.c

index 1d02da5..dc9910b 100644 (file)
@@ -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));