linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: liang xie <xieliang007@gmail.com>
Cc: acme@ghostprotocols.net, a.p.zijlstra@chello.nl, mingo@elte.hu,
	paulus@samba.org, balbi@ti.com, linux-perf-users@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] A trivial memory leak fix while calling system_path
Date: Fri, 07 Sep 2012 15:26:00 +0900	[thread overview]
Message-ID: <87oblibb7r.fsf@sejong.aot.lge.com> (raw)
In-Reply-To: <CADu=CFpMKkAW5+=Us9J2GOxZTBDN91FTeteCgbyh8c7fsP837g@mail.gmail.com> (liang xie's message of "Fri, 7 Sep 2012 11:34:49 +0800")

Hi, liang

On Fri, 7 Sep 2012 11:34:49 +0800, liang xie wrote:
> A trivial memory leak fix while calling system_path
>
> Since v1: Remove an unnecessary null pointer check per Felipe's comments
>
> Signed-off-by: Liang Xie <xieliang@xiaomi.com>
> ---
>  tools/perf/util/exec_cmd.c |    4 +++-
>  tools/perf/util/help.c     |    1 +
>  2 files changed, 4 insertions(+), 1 deletions(-)
>
> diff --git a/tools/perf/util/exec_cmd.c b/tools/perf/util/exec_cmd.c
> index 7adf4ad..d041407 100644
> --- a/tools/perf/util/exec_cmd.c
> +++ b/tools/perf/util/exec_cmd.c
> @@ -83,8 +83,9 @@ void setup_path(void)
>  {
>  	const char *old_path = getenv("PATH");
>  	struct strbuf new_path = STRBUF_INIT;
> +	const char *exec_path = perf_exec_path();
>
> -	add_path(&new_path, perf_exec_path());
> +	add_path(&new_path, exec_path);
>  	add_path(&new_path, argv0_path);
>
>  	if (old_path)
> @@ -95,6 +96,7 @@ void setup_path(void)
>  	setenv("PATH", new_path.buf, 1);
>
>  	strbuf_release(&new_path);
> +	free((void *)exec_path);
>  }

The problem is that perf_exec_path() can return a non-dynamically
allocated string (e.g. command line argument or environment string) and
currently we cannot know where the returned string is came from.

It might cause much more troubles when you free that kind of string than
just leaking a couple of bytes IMHO.

Thanks,
Namhyung

>
>  static const char **prepare_perf_cmd(const char **argv)
> diff --git a/tools/perf/util/help.c b/tools/perf/util/help.c
> index 6f2975a..798f66d 100644
> --- a/tools/perf/util/help.c
> +++ b/tools/perf/util/help.c
> @@ -187,6 +187,7 @@ void load_command_list(const char *prefix,
>  		uniq(other_cmds);
>  	}
>  	exclude_cmds(other_cmds, main_cmds);
> +	free((void *)exec_path);
>  }
>
>  void list_commands(const char *title, struct cmdnames *main_cmds,

  reply	other threads:[~2012-09-07  6:33 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-07  3:34 [PATCH v2] A trivial memory leak fix while calling system_path liang xie
2012-09-07  6:26 ` Namhyung Kim [this message]
2012-09-07 15:30   ` 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=87oblibb7r.fsf@sejong.aot.lge.com \
    --to=namhyung@kernel.org \
    --cc=a.p.zijlstra@chello.nl \
    --cc=acme@ghostprotocols.net \
    --cc=balbi@ti.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=paulus@samba.org \
    --cc=xieliang007@gmail.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).