All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] perf expr: Fix missing check for return value of hashmap__new
@ 2021-12-12  6:25 Miaoqian Lin
  2021-12-13 13:06 ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 3+ messages in thread
From: Miaoqian Lin @ 2021-12-12  6:25 UTC (permalink / raw)
  Cc: linmq006, Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Mark Rutland, Alexander Shishkin, Jiri Olsa, Namhyung Kim,
	Ian Rogers, Andi Kleen, linux-perf-users, linux-kernel

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@gmail.com>
---
 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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] perf expr: Fix missing check for return value of hashmap__new
  2021-12-12  6:25 [PATCH] perf expr: Fix missing check for return value of hashmap__new Miaoqian Lin
@ 2021-12-13 13:06 ` Arnaldo Carvalho de Melo
  2021-12-13 13:08   ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 3+ messages in thread
From: Arnaldo Carvalho de Melo @ 2021-12-13 13:06 UTC (permalink / raw)
  To: Miaoqian Lin
  Cc: Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Ian Rogers, Andi Kleen, linux-perf-users,
	linux-kernel

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@gmail.com>

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] perf expr: Fix missing check for return value of hashmap__new
  2021-12-13 13:06 ` Arnaldo Carvalho de Melo
@ 2021-12-13 13:08   ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 3+ messages in thread
From: Arnaldo Carvalho de Melo @ 2021-12-13 13:08 UTC (permalink / raw)
  To: Miaoqian Lin
  Cc: Peter Zijlstra, Ingo Molnar, Mark Rutland, Alexander Shishkin,
	Jiri Olsa, Namhyung Kim, Ian Rogers, Andi Kleen, linux-perf-users,
	linux-kernel

Em Mon, Dec 13, 2021 at 10:06:02AM -0300, Arnaldo Carvalho de Melo escreveu:
> 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@gmail.com>
> 
> Thanks, applied.

I take that back, kfree()? Are you compile testing this?

  CC      /tmp/build/perf/util/expr.o
util/expr.c: In function ‘expr__ctx_new’:
util/expr.c:302:13: error: implicit declaration of function ‘IS_ERR’ [-Werror=implicit-function-declaration]
  302 |         if (IS_ERR(ctx->ids)) {
      |             ^~~~~~
util/expr.c:303:17: error: implicit declaration of function ‘kfree’; did you mean ‘free’? [-Werror=implicit-function-declaration]
  303 |                 kfree(ctx);
      |                 ^~~~~
      |                 free
cc1: all warnings being treated as errors

I had to fix it applying this on top, please compile test your patches.

- Arnaldo

diff --git a/tools/perf/util/expr.c b/tools/perf/util/expr.c
index c94c9ea30d1abd6a..254601060b392c42 100644
--- a/tools/perf/util/expr.c
+++ b/tools/perf/util/expr.c
@@ -12,6 +12,7 @@
 #include "expr-bison.h"
 #include "expr-flex.h"
 #include "smt.h"
+#include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/zalloc.h>
 #include <ctype.h>
@@ -300,7 +301,7 @@ struct expr_parse_ctx *expr__ctx_new(void)
 
 	ctx->ids = hashmap__new(key_hash, key_equal, NULL);
 	if (IS_ERR(ctx->ids)) {
-		kfree(ctx);
+		free(ctx);
 		return NULL;
 	}
 	ctx->runtime = 0;

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-12-13 13:08 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-12-12  6:25 [PATCH] perf expr: Fix missing check for return value of hashmap__new Miaoqian Lin
2021-12-13 13:06 ` Arnaldo Carvalho de Melo
2021-12-13 13:08   ` Arnaldo Carvalho de Melo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.