linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Riccardo Mancini <rickyman7@gmail.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Riccardo Mancini <rickyman7@gmail.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Mark Rutland <mark.rutland@arm.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Jiri Olsa <jolsa@redhat.com>, Leo Yan <leo.yan@linaro.org>,
	Remi Bernon <rbernon@codeweavers.com>,
	Fabian Hemmer <copy@copy.sh>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] perf test: add missing free for scandir-returned dirent entities
Date: Fri,  9 Jul 2021 18:34:53 +0200	[thread overview]
Message-ID: <20210709163454.672082-1-rickyman7@gmail.com> (raw)

ASan reported a memory leak for items of the entlist returned from scandir.
In fact, scandir returns a malloc'd array of malloc'd dirents.
This patch adds the missing (z)frees.

Fixes: da963834fe6975a1 ("perf test: Iterate over shell tests in alphabetical order")
Signed-off-by: Riccardo Mancini <rickyman7@gmail.com>
---
 tools/perf/tests/builtin-test.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index 41e3cf6bb66c68e8..5e6242576236325c 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -26,6 +26,7 @@
 #include <linux/kernel.h>
 #include <linux/string.h>
 #include <subcmd/exec-cmd.h>
+#include <linux/zalloc.h>
 
 static bool dont_fork;
 
@@ -540,7 +541,7 @@ static int shell_tests__max_desc_width(void)
 {
 	struct dirent **entlist;
 	struct dirent *ent;
-	int n_dirs;
+	int n_dirs, e;
 	char path_dir[PATH_MAX];
 	const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
 	int width = 0;
@@ -564,8 +565,9 @@ static int shell_tests__max_desc_width(void)
 		}
 	}
 
+	for (e = 0; e < n_dirs; e++)
+		zfree(&entlist[e]);
 	free(entlist);
-
 	return width;
 }
 
@@ -596,7 +598,7 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width)
 {
 	struct dirent **entlist;
 	struct dirent *ent;
-	int n_dirs;
+	int n_dirs, e;
 	char path_dir[PATH_MAX];
 	struct shell_test st = {
 		.dir = shell_tests__dir(path_dir, sizeof(path_dir)),
@@ -629,6 +631,8 @@ static int run_shell_tests(int argc, const char *argv[], int i, int width)
 		test_and_print(&test, false, -1);
 	}
 
+	for (e = 0; e < n_dirs; e++)
+		zfree(&entlist[e]);
 	free(entlist);
 	return 0;
 }
@@ -730,7 +734,7 @@ static int perf_test__list_shell(int argc, const char **argv, int i)
 {
 	struct dirent **entlist;
 	struct dirent *ent;
-	int n_dirs;
+	int n_dirs, e;
 	char path_dir[PATH_MAX];
 	const char *path = shell_tests__dir(path_dir, sizeof(path_dir));
 
@@ -752,8 +756,11 @@ static int perf_test__list_shell(int argc, const char **argv, int i)
 			continue;
 
 		pr_info("%2d: %s\n", i, t.desc);
+
 	}
 
+	for (e = 0; e < n_dirs; e++)
+		zfree(&entlist[e]);
 	free(entlist);
 	return 0;
 }
-- 
2.23.0


             reply	other threads:[~2021-07-09 16:35 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-09 16:34 Riccardo Mancini [this message]
2021-07-09 18:21 ` [PATCH] perf test: add missing free for scandir-returned dirent entities Arnaldo Carvalho de Melo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210709163454.672082-1-rickyman7@gmail.com \
    --to=rickyman7@gmail.com \
    --cc=acme@kernel.org \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=copy@copy.sh \
    --cc=irogers@google.com \
    --cc=jolsa@redhat.com \
    --cc=leo.yan@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rbernon@codeweavers.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).