Re: [PATCH 1/1] perf callchain: Use zfree() to avoid possibly accessing dangling pointers

From: Ian Rogers
Date: Wed May 08 2024 - 19:40:36 EST


On Mon, May 6, 2024 at 8:12 PM Arnaldo Carvalho de Melo <acme@xxxxxxxxxx> wrote:
>
> When freeing a->b it is good practice to set a->b to NULL using
> zfree(&a->b) so that when we have a bug where a reference to a freed 'a'
> pointer is kept somewhere, we can more quickly cause a segfault if some
> code tries to use a->b.
>
> Convert one such case in the callchain code.
>
> Cc: Adrian Hunter <adrian.hunter@xxxxxxxxx>
> Cc: Ian Rogers <irogers@xxxxxxxxxx>
> Cc: Jiri Olsa <jolsa@xxxxxxxxxx>
> Cc: Kan Liang <kan.liang@xxxxxxxxxxxxxxx>
> Cc: Namhyung Kim <namhyung@xxxxxxxxxx>
> Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>

Reviewed-by: Ian Rogers <irogers@xxxxxxxxxx>

Thanks,
Ian

> ---
> tools/perf/util/callchain.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
> index 68feed8718096f78..1730b852a947400f 100644
> --- a/tools/perf/util/callchain.c
> +++ b/tools/perf/util/callchain.c
> @@ -606,7 +606,7 @@ fill_node(struct callchain_node *node, struct callchain_cursor *cursor)
> call->brtype_stat = zalloc(sizeof(*call->brtype_stat));
> if (!call->brtype_stat) {
> perror("not enough memory for the code path branch statistics");
> - free(call->brtype_stat);
> + zfree(&call->brtype_stat);
> return -ENOMEM;
> }
> }
> --
> 2.44.0
>