All of lore.kernel.org
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Dapeng Mi <dapeng1.mi@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	Ian Rogers <irogers@google.com>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Alexander Shishkin <alexander.shishkin@linux.intel.com>,
	Kan Liang <kan.liang@linux.intel.com>,
	linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org,
	Kevin Tian <kevin.tian@intel.com>,
	Dapeng Mi <dapeng1.mi@intel.com>
Subject: Re: [PATCH 1/5] perf tools kvm: Add missed memory allocation check and free
Date: Wed, 6 Aug 2025 16:40:05 -0700	[thread overview]
Message-ID: <aJPn1Z5zxqOjZ-Ym@google.com> (raw)
In-Reply-To: <20250805004633.135904-2-dapeng1.mi@linux.intel.com>

On Tue, Aug 05, 2025 at 08:46:29AM +0800, Dapeng Mi wrote:
> Current code allocates rec_argv[] array, but doesn't check if the
> allocation is successful and explicitly free the rec_argv[] array.
> 
> Add them back.
> 
> Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
> ---
>  tools/perf/builtin-kvm.c | 29 +++++++++++++++++++++++------
>  1 file changed, 23 insertions(+), 6 deletions(-)
> 
> diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
> index 7b15b4a705e4..f78a67a199ff 100644
> --- a/tools/perf/builtin-kvm.c
> +++ b/tools/perf/builtin-kvm.c
> @@ -1719,7 +1719,9 @@ kvm_events_record(struct perf_kvm_stat *kvm, int argc, const char **argv)
>  	set_option_flag(record_options, 0, "transaction", PARSE_OPT_DISABLED);
>  
>  	record_usage = kvm_stat_record_usage;
> -	return cmd_record(i, rec_argv);
> +	ret = cmd_record(i, rec_argv);
> +	free(rec_argv);

Well.. it's not enough just to free rec_argv.  You also need to free all
items in the rec_argv[].  Probably you want to add more STRDUP_FAIL_EXIT
when copying the original argv (here and other places).

Thanks,
Namhyung


> +	return ret;
>  }
>  
>  static int
> @@ -2006,6 +2008,9 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
>  
>  	rec_argc = argc + 2;
>  	rec_argv = calloc(rec_argc + 1, sizeof(char *));
> +	if (!rec_argv)
> +		return -ENOMEM;
> +
>  	rec_argv[i++] = strdup("record");
>  	rec_argv[i++] = strdup("-o");
>  	rec_argv[i++] = strdup(file_name);
> @@ -2014,16 +2019,21 @@ static int __cmd_record(const char *file_name, int argc, const char **argv)
>  
>  	BUG_ON(i != rec_argc);
>  
> -	return cmd_record(i, rec_argv);
> +	ret = cmd_record(i, rec_argv);
> +	free(rec_argv);
> +	return ret;
>  }
>  
>  static int __cmd_report(const char *file_name, int argc, const char **argv)
>  {
> -	int rec_argc, i = 0, j;
> +	int rec_argc, i = 0, j, ret;
>  	const char **rec_argv;
>  
>  	rec_argc = argc + 2;
>  	rec_argv = calloc(rec_argc + 1, sizeof(char *));
> +	if (!rec_argv)
> +		return -ENOMEM;
> +
>  	rec_argv[i++] = strdup("report");
>  	rec_argv[i++] = strdup("-i");
>  	rec_argv[i++] = strdup(file_name);
> @@ -2032,17 +2042,22 @@ static int __cmd_report(const char *file_name, int argc, const char **argv)
>  
>  	BUG_ON(i != rec_argc);
>  
> -	return cmd_report(i, rec_argv);
> +	ret = cmd_report(i, rec_argv);
> +	free(rec_argv);
> +	return ret;
>  }
>  
>  static int
>  __cmd_buildid_list(const char *file_name, int argc, const char **argv)
>  {
> -	int rec_argc, i = 0, j;
> +	int rec_argc, i = 0, j, ret;
>  	const char **rec_argv;
>  
>  	rec_argc = argc + 2;
>  	rec_argv = calloc(rec_argc + 1, sizeof(char *));
> +	if (!rec_argv)
> +		return -ENOMEM;
> +
>  	rec_argv[i++] = strdup("buildid-list");
>  	rec_argv[i++] = strdup("-i");
>  	rec_argv[i++] = strdup(file_name);
> @@ -2051,7 +2066,9 @@ __cmd_buildid_list(const char *file_name, int argc, const char **argv)
>  
>  	BUG_ON(i != rec_argc);
>  
> -	return cmd_buildid_list(i, rec_argv);
> +	ret = cmd_buildid_list(i, rec_argv);
> +	free(rec_argv);
> +	return ret;
>  }
>  
>  int cmd_kvm(int argc, const char **argv)
> -- 
> 2.34.1
> 

  reply	other threads:[~2025-08-06 23:40 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-05  0:46 [PATCH 0/5] Perf kvm commands bug fix Dapeng Mi
2025-08-05  0:46 ` [PATCH 1/5] perf tools kvm: Add missed memory allocation check and free Dapeng Mi
2025-08-06 23:40   ` Namhyung Kim [this message]
2025-08-07  2:51     ` Mi, Dapeng
2025-08-05  0:46 ` [PATCH 2/5] perf tools kvm: Fix the potential out of range memory access issue Dapeng Mi
2025-08-06 23:53   ` Namhyung Kim
2025-08-07  2:52     ` Mi, Dapeng
2025-08-05  0:46 ` [PATCH 3/5] perf tools: Add helper x86__is_intel_cpu() Dapeng Mi
2025-08-05  0:46 ` [PATCH 4/5] perf tools kvm: Use "cycles" to sample guest for "kvm record" on Intel Dapeng Mi
2025-08-07  0:08   ` Namhyung Kim
2025-08-07  3:08     ` Mi, Dapeng
2025-08-08 22:10       ` Namhyung Kim
2025-08-11  5:35         ` Mi, Dapeng
2025-08-05  0:46 ` [PATCH 5/5] perf tools kvm: Use "cycles" to sample guest for "kvm top" " Dapeng Mi
2025-08-05  0:57   ` Mi, Dapeng
2025-08-05 11:32     ` Aditya Bodkhe
2025-08-06  0:31       ` Mi, Dapeng

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=aJPn1Z5zxqOjZ-Ym@google.com \
    --to=namhyung@kernel.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=alexander.shishkin@linux.intel.com \
    --cc=dapeng1.mi@intel.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=irogers@google.com \
    --cc=kan.liang@linux.intel.com \
    --cc=kevin.tian@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@redhat.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.