aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/bpf/hashmap.c
diff options
context:
space:
mode:
authorAndrii Nakryiko <[email protected]>2020-04-29 01:21:04 +0000
committerAlexei Starovoitov <[email protected]>2020-04-29 02:48:05 +0000
commit229bf8bf4d910510bc1a2fd0b89bd467cd71050d (patch)
treecddf013296aff40219e40f654f9c212bd1f070c9 /tools/lib/bpf/hashmap.c
parentselftests/bpf: Convert test_hashmap into test_progs test (diff)
downloadkernel-229bf8bf4d910510bc1a2fd0b89bd467cd71050d.tar.gz
kernel-229bf8bf4d910510bc1a2fd0b89bd467cd71050d.zip
libbpf: Fix memory leak and possible double-free in hashmap__clear
Fix memory leak in hashmap_clear() not freeing hashmap_entry structs for each of the remaining entries. Also NULL-out bucket list to prevent possible double-free between hashmap__clear() and hashmap__free(). Running test_progs-asan flavor clearly showed this problem. Reported-by: Alston Tang <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
Diffstat (limited to 'tools/lib/bpf/hashmap.c')
-rw-r--r--tools/lib/bpf/hashmap.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/tools/lib/bpf/hashmap.c b/tools/lib/bpf/hashmap.c
index 54c30c802070..cffb96202e0d 100644
--- a/tools/lib/bpf/hashmap.c
+++ b/tools/lib/bpf/hashmap.c
@@ -59,7 +59,14 @@ struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
void hashmap__clear(struct hashmap *map)
{
+ struct hashmap_entry *cur, *tmp;
+ int bkt;
+
+ hashmap__for_each_entry_safe(map, cur, tmp, bkt) {
+ free(cur);
+ }
free(map->buckets);
+ map->buckets = NULL;
map->cap = map->cap_bits = map->sz = 0;
}