[PATCH v2] perf annotate: Fix hashmap__new() error checking

Chen Ni posted 1 patch 1 month ago
tools/perf/util/annotate.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
[PATCH v2] perf annotate: Fix hashmap__new() error checking
Posted by Chen Ni 1 month ago
The hashmap__new() function never returns NULL, it returns error
pointers. Fix the error checking to match.

Additionally, set src->samples to NULL to prevent any later code from
accidentally using the error pointer.

Fixes: d3e7cad6f36d ("perf annotate: Add a hashmap for symbol histogram")
Reviewed-by: Ian Rogers <irogers@google.com>
Signed-off-by: Chen Ni <nichen@iscas.ac.cn>
---
Changes in v2:
- Set src->samples to NULL on error
- Update commit message
---
 tools/perf/util/annotate.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index 2e3522905046..63f0ee9d4c03 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -44,6 +44,7 @@
 #include "strbuf.h"
 #include <regex.h>
 #include <linux/bitops.h>
+#include <linux/err.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
 #include <linux/zalloc.h>
@@ -137,8 +138,10 @@ static int annotated_source__alloc_histograms(struct annotated_source *src,
 		return -1;
 
 	src->samples = hashmap__new(sym_hist_hash, sym_hist_equal, NULL);
-	if (src->samples == NULL)
+	if (IS_ERR(src->samples)) {
 		zfree(&src->histograms);
+		src->samples = NULL;
+	}
 
 	return src->histograms ? 0 : -1;
 }
-- 
2.25.1
Re: [PATCH v2] perf annotate: Fix hashmap__new() error checking
Posted by Arnaldo Carvalho de Melo 1 month ago
On Fri, Mar 06, 2026 at 11:56:48AM +0800, Chen Ni wrote:
> The hashmap__new() function never returns NULL, it returns error
> pointers. Fix the error checking to match.
> 
> Additionally, set src->samples to NULL to prevent any later code from
> accidentally using the error pointer.
> 
> Fixes: d3e7cad6f36d ("perf annotate: Add a hashmap for symbol histogram")
> Reviewed-by: Ian Rogers <irogers@google.com>
> Signed-off-by: Chen Ni <nichen@iscas.ac.cn>

Thanks, applied to perf-tools, for v7.0.

- Arnaldo

> ---
> Changes in v2:
> - Set src->samples to NULL on error
> - Update commit message
> ---
>  tools/perf/util/annotate.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
> index 2e3522905046..63f0ee9d4c03 100644
> --- a/tools/perf/util/annotate.c
> +++ b/tools/perf/util/annotate.c
> @@ -44,6 +44,7 @@
>  #include "strbuf.h"
>  #include <regex.h>
>  #include <linux/bitops.h>
> +#include <linux/err.h>
>  #include <linux/kernel.h>
>  #include <linux/string.h>
>  #include <linux/zalloc.h>
> @@ -137,8 +138,10 @@ static int annotated_source__alloc_histograms(struct annotated_source *src,
>  		return -1;
>  
>  	src->samples = hashmap__new(sym_hist_hash, sym_hist_equal, NULL);
> -	if (src->samples == NULL)
> +	if (IS_ERR(src->samples)) {
>  		zfree(&src->histograms);
> +		src->samples = NULL;
> +	}
>  
>  	return src->histograms ? 0 : -1;
>  }
> -- 
> 2.25.1