[PATCH] perf ftrace: Fix NULL vs IS_ERR() check in prepare_func_profile()

Chen Ni posted 1 patch 1 month ago
tools/perf/builtin-ftrace.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
[PATCH] perf ftrace: Fix NULL vs IS_ERR() check in prepare_func_profile()
Posted by Chen Ni 1 month ago
The hashmap__new() function never returns NULL, it returns error
pointers. Fix the error checking to match.

Fixes: 0f223813edd0 ("perf ftrace: Add 'profile' command")
Signed-off-by: Chen Ni <nichen@iscas.ac.cn>
---
 tools/perf/builtin-ftrace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
index 6b6eec65f93f..9e7e81a08606 100644
--- a/tools/perf/builtin-ftrace.c
+++ b/tools/perf/builtin-ftrace.c
@@ -18,6 +18,7 @@
 #include <poll.h>
 #include <ctype.h>
 #include <linux/capability.h>
+#include <linux/err.h>
 #include <linux/string.h>
 #include <sys/stat.h>
 
@@ -1209,7 +1210,7 @@ static int prepare_func_profile(struct perf_ftrace *ftrace)
 	ftrace->graph_verbose = 0;
 
 	ftrace->profile_hash = hashmap__new(profile_hash, profile_equal, NULL);
-	if (ftrace->profile_hash == NULL)
+	if (IS_ERR(ftrace->profile_hash))
 		return -ENOMEM;
 
 	return 0;
-- 
2.25.1
Re: [PATCH] perf ftrace: Fix NULL vs IS_ERR() check in prepare_func_profile()
Posted by Ian Rogers 1 month ago
On Wed, Mar 4, 2026 at 6:25 PM Chen Ni <nichen@iscas.ac.cn> wrote:
>
> The hashmap__new() function never returns NULL, it returns error
> pointers. Fix the error checking to match.
>
> Fixes: 0f223813edd0 ("perf ftrace: Add 'profile' command")
> Signed-off-by: Chen Ni <nichen@iscas.ac.cn>
> ---
>  tools/perf/builtin-ftrace.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/builtin-ftrace.c b/tools/perf/builtin-ftrace.c
> index 6b6eec65f93f..9e7e81a08606 100644
> --- a/tools/perf/builtin-ftrace.c
> +++ b/tools/perf/builtin-ftrace.c
> @@ -18,6 +18,7 @@
>  #include <poll.h>
>  #include <ctype.h>
>  #include <linux/capability.h>
> +#include <linux/err.h>
>  #include <linux/string.h>
>  #include <sys/stat.h>
>
> @@ -1209,7 +1210,7 @@ static int prepare_func_profile(struct perf_ftrace *ftrace)
>         ftrace->graph_verbose = 0;
>
>         ftrace->profile_hash = hashmap__new(profile_hash, profile_equal, NULL);
> -       if (ftrace->profile_hash == NULL)
> +       if (IS_ERR(ftrace->profile_hash))
>                 return -ENOMEM;

It's probably a good idea to make sure ftrace->profile_hash is NULL
here. It is probably more correct to return an PTR_ERR of the return
value from hashmap__new.

Thanks,
Ian

>
>         return 0;

> --
> 2.25.1
>