From e2b5bba185c96bf4ecddfe22d34ace02706122b4 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Mon, 7 Jan 2013 09:20:25 +0100 Subject: Guarantee that the key is freed when replaced * When setting a key in a map that already exists, then free the old key and replace with the new one. * Fix related bug where key was not properly allocated * Add tests for this https://bugs.freedesktop.org/show_bug.cgi?id=59087 --- p11-kit/hashmap.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'p11-kit/hashmap.c') diff --git a/p11-kit/hashmap.c b/p11-kit/hashmap.c index 1c4aff1..d420221 100644 --- a/p11-kit/hashmap.c +++ b/p11-kit/hashmap.c @@ -157,11 +157,16 @@ _p11_hash_set (hashmap *map, bucketp = lookup_or_create_bucket (map, key, 1); if(bucketp && *bucketp) { + /* Destroy the previous key */ + if ((*bucketp)->key && (*bucketp)->key != key && map->key_destroy_func) + map->key_destroy_func ((*bucketp)->key); + /* Destroy the previous value */ - if ((*bucketp)->value && map->value_destroy_func) + if ((*bucketp)->value && (*bucketp)->value != val && map->value_destroy_func) map->value_destroy_func ((*bucketp)->value); /* replace entry */ + (*bucketp)->key = key; (*bucketp)->value = val; /* check that the collision rate isn't too high */ -- cgit v1.1