From: Yunfeng Ye <yeyunfeng@huawei.com>
To: <peterz@infradead.org>, <mingo@redhat.com>, <acme@kernel.org>,
<mark.rutland@arm.com>, <alexander.shishkin@linux.intel.com>,
<jolsa@redhat.com>, <namhyung@kernel.org>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"hushiyuan@huawei.com" <hushiyuan@huawei.com>,
"linfeilong@huawei.com" <linfeilong@huawei.com>
Subject: [PATCH] perf kmem: Fix memory leak in __cmd_record()
Date: Mon, 28 Oct 2019 14:46:35 +0800 [thread overview]
Message-ID: <81e3d338-dbf3-341b-431d-27bb51996e46@huawei.com> (raw)
There are memory leaks in __cmd_record() found by visual inspection.
calloc() and strdup() are used to allocate memory for rec_argv pointer
array and argv, which should be freed before the function returns.
In addition, checking whether strdup() is success or not, if failure,
then go to the failure path to free memory and return the function.
Fixes: ba77c9e11111 ("perf: Add 'perf kmem' tool")
Signed-off-by: Yunfeng Ye <yeyunfeng@huawei.com>
---
tools/perf/builtin-kmem.c | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/tools/perf/builtin-kmem.c b/tools/perf/builtin-kmem.c
index 9661671cc26e..6a62acfc9470 100644
--- a/tools/perf/builtin-kmem.c
+++ b/tools/perf/builtin-kmem.c
@@ -1847,6 +1847,7 @@ static int __cmd_record(int argc, const char **argv)
};
unsigned int rec_argc, i, j;
const char **rec_argv;
+ int ret = -ENOMEM;
rec_argc = ARRAY_SIZE(record_args) + argc - 1;
if (kmem_slab)
@@ -1873,10 +1874,20 @@ static int __cmd_record(int argc, const char **argv)
rec_argv[i] = strdup(page_events[j]);
}
- for (j = 1; j < (unsigned int)argc; j++, i++)
- rec_argv[i] = argv[j];
+ for (j = 0; j < i; j++)
+ if (!rec_argv[j]) /* check strdup success or not */
+ goto out;
+
+ rec_argc = i;
+ for (j = 1; j < (unsigned int)argc; j++, rec_argc++)
+ rec_argv[rec_argc] = argv[j];
- return cmd_record(i, rec_argv);
+ ret = cmd_record(rec_argc, rec_argv);
+out:
+ for (i--; (int)i >= 0; i--)
+ free((void *)rec_argv[i]);
+ free(rec_argv);
+ return ret;
}
static int kmem_config(const char *var, const char *value, void *cb __maybe_unused)
--
2.7.4
reply other threads:[~2019-10-28 6:47 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=81e3d338-dbf3-341b-431d-27bb51996e46@huawei.com \
--to=yeyunfeng@huawei.com \
--cc=acme@kernel.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=hushiyuan@huawei.com \
--cc=jolsa@redhat.com \
--cc=linfeilong@huawei.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/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
all inboxes | Powered by JetHome®