From: Leandro Pereira Date: Fri, 12 Oct 2012 15:28:56 +0000 (-0300) Subject: libkmod-hash: Plug possible memory leak when free_value is defined X-Git-Tag: v11~33 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1faec2c1344fd2725152a6944dc5ac2525999ec6;p=thirdparty%2Fkmod.git 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. --- diff --git a/libkmod/libkmod-hash.c b/libkmod/libkmod-hash.c index 1d02da58..dc9910bd 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));