* [PATCH] perf tools: fix bug in usage of the basename() function
@ 2013-12-05 1:55 Stephane Eranian
2013-12-05 8:59 ` Ingo Molnar
0 siblings, 1 reply; 4+ messages in thread
From: Stephane Eranian @ 2013-12-05 1:55 UTC (permalink / raw)
To: linux-kernel; +Cc: acme, mingo, peterz, jolsa, bccheng, dsahern
The basename() implementation varies a lot between systems.
The Linux man page says: "basename may modify the content of the path,
so it may be desirable to pass a copy when calling the function".
On some other systems, the returned address may come from an internal
buffer which can be reused in subsequent calls, thus the results should
also be copied.
The dso__set_basename() function was not doing this causing problems
on some systems with wrong library names being shown by perf report,
such as on Android systems.
This patch fixes the problem.
Thanks to Ben Cheng for tracking down the problem.
Patch relative to tip.git at commit 631d5ea.
Reported-by: Ben Cheng <bccheng@google.com>
Signed-off-by: Stephane Eranian <eranian@google.com>
---
tools/perf/util/dso.c | 29 ++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
index af4c687c..d186ace 100644
--- a/tools/perf/util/dso.c
+++ b/tools/perf/util/dso.c
@@ -404,7 +404,34 @@ void dso__set_short_name(struct dso *dso, const char *name)
static void dso__set_basename(struct dso *dso)
{
- dso__set_short_name(dso, basename(dso->long_name));
+ char *lname, *base;
+
+ /*
+ * basename may modify path buffer, so we must pass
+ * a copy.
+ */
+ lname = strdup(dso->long_name);
+ if (!lname)
+ return;
+
+ /*
+ * basename may return pointer to internal
+ * storage which is reused in subsequent calls
+ * so copy the result
+ */
+ base = strdup(basename(lname));
+
+ free(lname);
+
+ if (!base)
+ return;
+
+ if (dso->sname_alloc)
+ free((char *)dso->short_name);
+ else
+ dso->sname_alloc = 1;
+
+ dso__set_short_name(dso, base);
}
int dso__name_len(const struct dso *dso)
--
1.7.9.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] perf tools: fix bug in usage of the basename() function
2013-12-05 1:55 [PATCH] perf tools: fix bug in usage of the basename() function Stephane Eranian
@ 2013-12-05 8:59 ` Ingo Molnar
2013-12-05 18:19 ` Stephane Eranian
0 siblings, 1 reply; 4+ messages in thread
From: Ingo Molnar @ 2013-12-05 8:59 UTC (permalink / raw)
To: Stephane Eranian
Cc: linux-kernel, acme, mingo, peterz, jolsa, bccheng, dsahern
* Stephane Eranian <eranian@google.com> wrote:
>
> The basename() implementation varies a lot between systems.
> The Linux man page says: "basename may modify the content of the path,
> so it may be desirable to pass a copy when calling the function".
> On some other systems, the returned address may come from an internal
> buffer which can be reused in subsequent calls, thus the results should
> also be copied.
>
> The dso__set_basename() function was not doing this causing problems
> on some systems with wrong library names being shown by perf report,
> such as on Android systems.
>
> This patch fixes the problem.
> Thanks to Ben Cheng for tracking down the problem.
>
> Patch relative to tip.git at commit 631d5ea.
>
> Reported-by: Ben Cheng <bccheng@google.com>
> Signed-off-by: Stephane Eranian <eranian@google.com>
> ---
Just three nits:
> tools/perf/util/dso.c | 29 ++++++++++++++++++++++++++++-
> 1 file changed, 28 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
> index af4c687c..d186ace 100644
> --- a/tools/perf/util/dso.c
> +++ b/tools/perf/util/dso.c
> @@ -404,7 +404,34 @@ void dso__set_short_name(struct dso *dso, const char *name)
>
> static void dso__set_basename(struct dso *dso)
> {
> - dso__set_short_name(dso, basename(dso->long_name));
> + char *lname, *base;
> +
> + /*
> + * basename may modify path buffer, so we must pass
> + * a copy.
s/basename may modify path buffer
/basename() may modify the path buffer
> + */
> + lname = strdup(dso->long_name);
> + if (!lname)
> + return;
> +
> + /*
> + * basename may return pointer to internal
> + * storage which is reused in subsequent calls
> + * so copy the result
> + */
s/basename may return pointer
/basename() may return a pointer
Makes for easier reading.
(Also please use consistent periods - the first comment has a period,
the second one doesn't. ':' works well too:)
> + base = strdup(basename(lname));
> +
> + free(lname);
> +
> + if (!base)
> + return;
> +
> + if (dso->sname_alloc)
> + free((char *)dso->short_name);
That cast is probably not needed.
> + else
> + dso->sname_alloc = 1;
> +
> + dso__set_short_name(dso, base);
> }
>
> int dso__name_len(const struct dso *dso)
Otherwise:
Acked-by: Ingo Molnar <mingo@kernel.org>
Thanks,
Ingo
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] perf tools: fix bug in usage of the basename() function
2013-12-05 8:59 ` Ingo Molnar
@ 2013-12-05 18:19 ` Stephane Eranian
2013-12-10 12:34 ` Ingo Molnar
0 siblings, 1 reply; 4+ messages in thread
From: Stephane Eranian @ 2013-12-05 18:19 UTC (permalink / raw)
To: Ingo Molnar
Cc: LKML, Arnaldo Carvalho de Melo, mingo@elte.hu, Peter Zijlstra,
Jiri Olsa, Ben Cheng, David Ahern
On Thu, Dec 5, 2013 at 9:59 AM, Ingo Molnar <mingo@kernel.org> wrote:
>
> * Stephane Eranian <eranian@google.com> wrote:
>
>>
>> The basename() implementation varies a lot between systems.
>> The Linux man page says: "basename may modify the content of the path,
>> so it may be desirable to pass a copy when calling the function".
>> On some other systems, the returned address may come from an internal
>> buffer which can be reused in subsequent calls, thus the results should
>> also be copied.
>>
>> The dso__set_basename() function was not doing this causing problems
>> on some systems with wrong library names being shown by perf report,
>> such as on Android systems.
>>
>> This patch fixes the problem.
>> Thanks to Ben Cheng for tracking down the problem.
>>
>> Patch relative to tip.git at commit 631d5ea.
>>
>> Reported-by: Ben Cheng <bccheng@google.com>
>> Signed-off-by: Stephane Eranian <eranian@google.com>
>> ---
>
> Just three nits:
>
>> tools/perf/util/dso.c | 29 ++++++++++++++++++++++++++++-
>> 1 file changed, 28 insertions(+), 1 deletion(-)
>>
>> diff --git a/tools/perf/util/dso.c b/tools/perf/util/dso.c
>> index af4c687c..d186ace 100644
>> --- a/tools/perf/util/dso.c
>> +++ b/tools/perf/util/dso.c
>> @@ -404,7 +404,34 @@ void dso__set_short_name(struct dso *dso, const char *name)
>>
>> static void dso__set_basename(struct dso *dso)
>> {
>> - dso__set_short_name(dso, basename(dso->long_name));
>> + char *lname, *base;
>> +
>> + /*
>> + * basename may modify path buffer, so we must pass
>> + * a copy.
>
> s/basename may modify path buffer
> /basename() may modify the path buffer
>
>> + */
>> + lname = strdup(dso->long_name);
>> + if (!lname)
>> + return;
>> +
>> + /*
>> + * basename may return pointer to internal
>> + * storage which is reused in subsequent calls
>> + * so copy the result
>> + */
>
> s/basename may return pointer
> /basename() may return a pointer
>
> Makes for easier reading.
>
> (Also please use consistent periods - the first comment has a period,
> the second one doesn't. ':' works well too:)
>
Fixed that.
>
>> + base = strdup(basename(lname));
>> +
>> + free(lname);
>> +
>> + if (!base)
>> + return;
>> +
>> + if (dso->sname_alloc)
>> + free((char *)dso->short_name);
>
> That cast is probably not needed.
>
It is with my compiler. It prints out a warning otherwise.
>> + else
>> + dso->sname_alloc = 1;
>> +
>> + dso__set_short_name(dso, base);
>> }
>>
>> int dso__name_len(const struct dso *dso)
>
> Otherwise:
>
> Acked-by: Ingo Molnar <mingo@kernel.org>
>
> Thanks,
>
> Ingo
Reposting ASAP.
Thanks.
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] perf tools: fix bug in usage of the basename() function
2013-12-05 18:19 ` Stephane Eranian
@ 2013-12-10 12:34 ` Ingo Molnar
0 siblings, 0 replies; 4+ messages in thread
From: Ingo Molnar @ 2013-12-10 12:34 UTC (permalink / raw)
To: Stephane Eranian
Cc: LKML, Arnaldo Carvalho de Melo, mingo@elte.hu, Peter Zijlstra,
Jiri Olsa, Ben Cheng, David Ahern
* Stephane Eranian <eranian@google.com> wrote:
> >> + base = strdup(basename(lname));
> >> +
> >> + free(lname);
> >> +
> >> + if (!base)
> >> + return;
> >> +
> >> + if (dso->sname_alloc)
> >> + free((char *)dso->short_name);
> >
> > That cast is probably not needed.
> >
> It is with my compiler. It prints out a warning otherwise.
Yeah, see my previous mail, I think having dso->short_name as 'const'
is a mistake, as there are really just two main usecases for methods
that operate on 'struct dso':
- life time affecting (setup/free) methods which need access to all
fields, which don't want dso->short_name as a const (as evidenced
by the cast).
- actual usage methods that get a 'const struct dso' anyway, so they
don't need dso->short_name as a const.
Thanks,
Ingo
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2013-12-10 12:34 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-12-05 1:55 [PATCH] perf tools: fix bug in usage of the basename() function Stephane Eranian
2013-12-05 8:59 ` Ingo Molnar
2013-12-05 18:19 ` Stephane Eranian
2013-12-10 12:34 ` Ingo Molnar
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox