diff mbox series

[2/2] lib/bpf hashmap: fixes to hashmap__clear

Message ID 20200506205257.8964-3-irogers@google.com
State Changes Requested
Delegated to: BPF Maintainers
Headers show
Series lib/bpf hashmap portability and fix | expand

Commit Message

Ian Rogers May 6, 2020, 8:52 p.m. UTC
hashmap_find_entry assumes that if buckets is NULL then there are no
entries. NULL the buckets in clear to ensure this.
Free hashmap entries and not just the bucket array.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/lib/bpf/hashmap.c | 6 ++++++
 1 file changed, 6 insertions(+)

Comments

Andrii Nakryiko May 6, 2020, 9:36 p.m. UTC | #1
On Wed, May 6, 2020 at 1:55 PM Ian Rogers <irogers@google.com> wrote:
>
> hashmap_find_entry assumes that if buckets is NULL then there are no
> entries. NULL the buckets in clear to ensure this.
> Free hashmap entries and not just the bucket array.
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---

This is already fixed in bpf-next ([0]). Seems to be 1-to-1 character
by character :)

  [0] https://patchwork.ozlabs.org/project/netdev/patch/20200429012111.277390-5-andriin@fb.com/

>  tools/lib/bpf/hashmap.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/tools/lib/bpf/hashmap.c b/tools/lib/bpf/hashmap.c
> index 54c30c802070..1a1bca1ff5cd 100644
> --- a/tools/lib/bpf/hashmap.c
> +++ b/tools/lib/bpf/hashmap.c
> @@ -59,7 +59,13 @@ struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
>
>  void hashmap__clear(struct hashmap *map)
>  {
> +       struct hashmap_entry *cur, *tmp;
> +       size_t 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;
>  }
>
> --
> 2.26.2.526.g744177e7f7-goog
>
Ian Rogers May 6, 2020, 9:53 p.m. UTC | #2
On Wed, May 6, 2020 at 2:36 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Wed, May 6, 2020 at 1:55 PM Ian Rogers <irogers@google.com> wrote:
> >
> > hashmap_find_entry assumes that if buckets is NULL then there are no
> > entries. NULL the buckets in clear to ensure this.
> > Free hashmap entries and not just the bucket array.
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
>
> This is already fixed in bpf-next ([0]). Seems to be 1-to-1 character
> by character :)
>
>   [0] https://patchwork.ozlabs.org/project/netdev/patch/20200429012111.277390-5-andriin@fb.com/

Thanks!
Ian

> >  tools/lib/bpf/hashmap.c | 6 ++++++
> >  1 file changed, 6 insertions(+)
> >
> > diff --git a/tools/lib/bpf/hashmap.c b/tools/lib/bpf/hashmap.c
> > index 54c30c802070..1a1bca1ff5cd 100644
> > --- a/tools/lib/bpf/hashmap.c
> > +++ b/tools/lib/bpf/hashmap.c
> > @@ -59,7 +59,13 @@ struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
> >
> >  void hashmap__clear(struct hashmap *map)
> >  {
> > +       struct hashmap_entry *cur, *tmp;
> > +       size_t 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;
> >  }
> >
> > --
> > 2.26.2.526.g744177e7f7-goog
> >
diff mbox series

Patch

diff --git a/tools/lib/bpf/hashmap.c b/tools/lib/bpf/hashmap.c
index 54c30c802070..1a1bca1ff5cd 100644
--- a/tools/lib/bpf/hashmap.c
+++ b/tools/lib/bpf/hashmap.c
@@ -59,7 +59,13 @@  struct hashmap *hashmap__new(hashmap_hash_fn hash_fn,
 
 void hashmap__clear(struct hashmap *map)
 {
+	struct hashmap_entry *cur, *tmp;
+	size_t 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;
 }