[PATCH v2 3/3] perf c2c: Prevent potential memory leak in c2c_he_zalloc

Shang XiaoJing posted 3 patches 3 years, 7 months ago
[PATCH v2 3/3] perf c2c: Prevent potential memory leak in c2c_he_zalloc
Posted by Shang XiaoJing 3 years, 7 months ago
Free allocated resources when zalloc is failed for members in c2c_he, to
prevent potential memory leak in c2c_he_zalloc.

Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
Acked-by: Jiri Olsa <jolsa@kernel.org>
---
changes in v2:
- clear omissible free labels due to zero allocated c2c_he.
---
 tools/perf/builtin-c2c.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/tools/perf/builtin-c2c.c b/tools/perf/builtin-c2c.c
index 12f272811487..f35a47b2dbe4 100644
--- a/tools/perf/builtin-c2c.c
+++ b/tools/perf/builtin-c2c.c
@@ -146,15 +146,15 @@ static void *c2c_he_zalloc(size_t size)
 
 	c2c_he->cpuset = bitmap_zalloc(c2c.cpus_cnt);
 	if (!c2c_he->cpuset)
-		return NULL;
+		goto out_free;
 
 	c2c_he->nodeset = bitmap_zalloc(c2c.nodes_cnt);
 	if (!c2c_he->nodeset)
-		return NULL;
+		goto out_free;
 
 	c2c_he->node_stats = zalloc(c2c.nodes_cnt * sizeof(*c2c_he->node_stats));
 	if (!c2c_he->node_stats)
-		return NULL;
+		goto out_free;
 
 	init_stats(&c2c_he->cstats.lcl_hitm);
 	init_stats(&c2c_he->cstats.rmt_hitm);
@@ -163,6 +163,12 @@ static void *c2c_he_zalloc(size_t size)
 	init_stats(&c2c_he->cstats.load);
 
 	return &c2c_he->he;
+
+out_free:
+	free(c2c_he->nodeset);
+	free(c2c_he->cpuset);
+	free(c2c_he);
+	return NULL;
 }
 
 static void c2c_he_free(void *he)
-- 
2.17.1
Re: [PATCH v2 3/3] perf c2c: Prevent potential memory leak in c2c_he_zalloc
Posted by Leo Yan 3 years, 7 months ago
On Tue, Sep 06, 2022 at 11:29:06AM +0800, Shang XiaoJing wrote:
> Free allocated resources when zalloc is failed for members in c2c_he, to
> prevent potential memory leak in c2c_he_zalloc.
> 
> Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
> Acked-by: Jiri Olsa <jolsa@kernel.org>

Reviewed-by: Leo Yan <leo.yan@linaro.org>
Re: [PATCH v2 3/3] perf c2c: Prevent potential memory leak in c2c_he_zalloc
Posted by Arnaldo Carvalho de Melo 3 years, 7 months ago
Em Tue, Sep 06, 2022 at 12:31:42PM +0800, Leo Yan escreveu:
> On Tue, Sep 06, 2022 at 11:29:06AM +0800, Shang XiaoJing wrote:
> > Free allocated resources when zalloc is failed for members in c2c_he, to
> > prevent potential memory leak in c2c_he_zalloc.
> > 
> > Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
> > Acked-by: Jiri Olsa <jolsa@kernel.org>
> 
> Reviewed-by: Leo Yan <leo.yan@linaro.org>

Thanks, applied to perf/urgent.

- Arnaldo