linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] A trivial memory leak fix while calling system_path
@ 2012-09-07  3:34 liang xie
  2012-09-07  6:26 ` Namhyung Kim
  0 siblings, 1 reply; 3+ messages in thread
From: liang xie @ 2012-09-07  3:34 UTC (permalink / raw)
  To: acme; +Cc: a.p.zijlstra, mingo, paulus, balbi, linux-perf-users, linux-kernel

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);
 }

 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,
-- 
1.7.1

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] A trivial memory leak fix while calling system_path
  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
  2012-09-07 15:30   ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 3+ messages in thread
From: Namhyung Kim @ 2012-09-07  6:26 UTC (permalink / raw)
  To: liang xie
  Cc: acme, a.p.zijlstra, mingo, paulus, balbi, linux-perf-users, linux-kernel

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,

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] A trivial memory leak fix while calling system_path
  2012-09-07  6:26 ` Namhyung Kim
@ 2012-09-07 15:30   ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 3+ messages in thread
From: Arnaldo Carvalho de Melo @ 2012-09-07 15:30 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: liang xie, a.p.zijlstra, mingo, paulus, balbi, linux-perf-users,
	linux-kernel

Em Fri, Sep 07, 2012 at 03:26:00PM +0900, Namhyung Kim escreveu:
> On Fri, 7 Sep 2012 11:34:49 +0800, liang xie wrote:
> > +	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.

And that is just gross, ugh. Make it always return dynamically allocated
string, not const, drop the cast on free and be done with it :-)

- Arnaldo

> 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,

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-09-07 15:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2012-09-07 15:30   ` Arnaldo Carvalho de Melo

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).