Jim Meyering <jim <at> meyering.net> writes: > > Aargh. Ten minutes after I push, I finally see my memory leak. > > > > Obviously, new_table needs to be freed before returning true. > > Hah! I should have looked at more than the patch. > > Is that code path already exercised by test-hash.c? > If so, I would have seen it eventually, via valgrind.
Yes, for example, look at op 6 in the random hash ops section (although can you truly trust rand() to be predictable across systems?). A valgrind run would indeed catch it (but that's too mechanical - it's more fun to find memory leaks by code inspection ;) Here's what I'll push: From: Eric Blake <e...@byu.net> Date: Thu, 18 Jun 2009 15:24:38 -0600 Subject: [PATCH] hash: fix memory leak in last patch * lib/hash.c (hash_rehash): Avoid memory leak. Signed-off-by: Eric Blake <e...@byu.net> --- ChangeLog | 3 +++ lib/hash.c | 5 ++++- 2 files changed, 7 insertions(+), 1 deletions(-) diff --git a/ChangeLog b/ChangeLog index e7698b7..36027fa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2009-06-18 Eric Blake <e...@byu.net> + hash: fix memory leak in last patch + * lib/hash.c (hash_rehash): Avoid memory leak. + hash: avoid no-op rehashing * lib/hash.c (hash_rehash): Recognize useless rehash attempts. diff --git a/lib/hash.c b/lib/hash.c index f2123b4..88ae32c 100644 --- a/lib/hash.c +++ b/lib/hash.c @@ -863,7 +863,10 @@ hash_rehash (Hash_table *table, size_t candidate) if (new_table == NULL) return false; if (new_table->n_buckets == table->n_buckets) - return true; + { + free (new_table); + return true; + } /* Merely reuse the extra old space into the new table. */ #if USE_OBSTACK -- 1.6.3.2