Re: [PATCH] perf expr: Fix missing check for return value of hashmap__new

From: Arnaldo Carvalho de Melo
Date: Mon Dec 13 2021 - 08:06:10 EST


Em Sun, Dec 12, 2021 at 06:25:02AM +0000, Miaoqian Lin escreveu:
> The hashmap__new() function may return ERR_PTR(-ENOMEM) when malloc
> fails, add IS_ERR checking for ctx->ids.
>
> Signed-off-by: Miaoqian Lin <linmq006@xxxxxxxxx>

Thanks, applied.

As a follow up you may consider using ids__new() instead:

struct hashmap *ids__new(void)
{
return hashmap__new(key_hash, key_equal, NULL);
}

And I noticed that the users of ids__new() are not using IS_ERR() on its
return in tools/perf/tests/expr.c, e.g.:

static int test_ids_union(void)
{
struct hashmap *ids1, *ids2;

/* Empty union. */
ids1 = ids__new();
TEST_ASSERT_VAL("ids__new", ids1);
ids2 = ids__new();
TEST_ASSERT_VAL("ids__new", ids2);

This needs converting to TEST_ASSERT_VAL_IS_ERR() probably after
introducing it, I haven't checked if this variation exists.

- Arnaldo

> ---
> tools/perf/util/expr.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c
> index 1d532b9fed29..c94c9ea30d1a 100644
> --- a/tools/perf/util/expr.c
> +++ b/tools/perf/util/expr.c
> @@ -299,6 +299,10 @@ struct expr_parse_ctx *expr__ctx_new(void)
> return NULL;
>
> ctx->ids = hashmap__new(key_hash, key_equal, NULL);
> + if (IS_ERR(ctx->ids)) {
> + kfree(ctx);
> + return NULL;
> + }
> ctx->runtime = 0;
>
> return ctx;
> --
> 2.17.1

--

- Arnaldo