diff options
author | Antoine Labour <piman@chromium.org> | 2012-05-24 18:17:50 -0700 |
---|---|---|
committer | Stéphane Marchesin <marcheu@chromium.org> | 2012-06-12 14:42:22 -0700 |
commit | 3c9fab88226af8360817c01ecde38348284e6ce7 (patch) | |
tree | 18147c9d3a87fd1ade9ae1043d131e9ff7200baf /src | |
parent | e2e9b4b10fcf3ba6358b9be54638f850523af82e (diff) | |
download | external_mesa3d-3c9fab88226af8360817c01ecde38348284e6ce7.zip external_mesa3d-3c9fab88226af8360817c01ecde38348284e6ce7.tar.gz external_mesa3d-3c9fab88226af8360817c01ecde38348284e6ce7.tar.bz2 |
mesa: Fix hash table leak
When a value was replaced, the new key was strdup'd and leaked.
To fix this, we modify the hash table implementation to return
whether the value was replaced and free() the (now useless)
duplicate string.
Diffstat (limited to 'src')
-rw-r--r-- | src/mesa/program/hash_table.c | 5 | ||||
-rw-r--r-- | src/mesa/program/hash_table.h | 16 |
2 files changed, 15 insertions, 6 deletions
diff --git a/src/mesa/program/hash_table.c b/src/mesa/program/hash_table.c index dc8563a..7dabadc 100644 --- a/src/mesa/program/hash_table.c +++ b/src/mesa/program/hash_table.c @@ -149,7 +149,7 @@ hash_table_insert(struct hash_table *ht, void *data, const void *key) insert_at_head(& ht->buckets[bucket], & node->link); } -void +bool hash_table_replace(struct hash_table *ht, void *data, const void *key) { const unsigned hash_value = (*ht->hash)(key); @@ -162,7 +162,7 @@ hash_table_replace(struct hash_table *ht, void *data, const void *key) if ((*ht->compare)(hn->key, key) == 0) { hn->data = data; - return; + return true; } } @@ -172,6 +172,7 @@ hash_table_replace(struct hash_table *ht, void *data, const void *key) hn->key = key; insert_at_head(& ht->buckets[bucket], & hn->link); + return false; } void diff --git a/src/mesa/program/hash_table.h b/src/mesa/program/hash_table.h index bcf65df..a14bf13 100644 --- a/src/mesa/program/hash_table.h +++ b/src/mesa/program/hash_table.h @@ -114,6 +114,10 @@ extern void hash_table_insert(struct hash_table *ht, void *data, /** * Add an element to a hash table with replacement * + * \return + * 1 if it did replace the the value (in which case the old key is kept), 0 if + * it did not replace the value (in which case the new key is kept). + * * \warning * If \c key is already in the hash table, \c data will \b replace the most * recently inserted \c data (see the warning in \c hash_table_insert) for @@ -121,7 +125,7 @@ extern void hash_table_insert(struct hash_table *ht, void *data, * * \sa hash_table_insert */ -extern void hash_table_replace(struct hash_table *ht, void *data, +extern int hash_table_replace(struct hash_table *ht, void *data, const void *key); /** @@ -219,6 +223,7 @@ public: */ void clear() { + hash_table_call_foreach(this->ht, delete_key, NULL); hash_table_clear(this->ht); } @@ -258,9 +263,12 @@ public: * because UINT_MAX+1 = 0. */ assert(value != UINT_MAX); - hash_table_replace(this->ht, - (void *) (intptr_t) (value + 1), - strdup(key)); + char *dup_key = strdup(key); + int result = hash_table_replace(this->ht, + (void *) (intptr_t) (value + 1), + dup_key); + if (result) + free(dup_key); } private: |