* [PATCH] tools: perf: util: parse-events.c: Cleaning up missing null-terminate after strncpy call
@ 2014-06-04 21:46 Rickard Strandqvist
2014-06-04 22:20 ` Andi Kleen
0 siblings, 1 reply; 6+ messages in thread
From: Rickard Strandqvist @ 2014-06-04 21:46 UTC (permalink / raw)
To: Peter Zijlstra, Paul Mackerras
Cc: Rickard Strandqvist, Ingo Molnar, Arnaldo Carvalho de Melo,
Jiri Olsa, Namhyung Kim, Andi Kleen, Adrian Hunter,
Stephane Eranian, David Ahern, Vince Weaver, linux-kernel
Added a guaranteed null-terminate after call to strncpy.
This was partly found using a static code analysis program called cppcheck.
Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
---
tools/perf/util/parse-events.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
index 1e15df1..fad2976 100644
--- a/tools/perf/util/parse-events.c
+++ b/tools/perf/util/parse-events.c
@@ -1216,8 +1216,10 @@ static void print_symbol_events(const char *event_glob, unsigned type,
if (strlen(syms->alias))
snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
- else
+ else {
strncpy(name, syms->symbol, MAX_NAME_LEN);
+ name[sizeof(name) - 1] = '\0';
+ }
printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
--
1.7.10.4
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH] tools: perf: util: parse-events.c: Cleaning up missing null-terminate after strncpy call
2014-06-04 21:46 [PATCH] tools: perf: util: parse-events.c: Cleaning up missing null-terminate after strncpy call Rickard Strandqvist
@ 2014-06-04 22:20 ` Andi Kleen
2014-06-04 22:23 ` Rickard Strandqvist
0 siblings, 1 reply; 6+ messages in thread
From: Andi Kleen @ 2014-06-04 22:20 UTC (permalink / raw)
To: Rickard Strandqvist
Cc: Peter Zijlstra, Paul Mackerras, Ingo Molnar,
Arnaldo Carvalho de Melo, Jiri Olsa, Namhyung Kim, Adrian Hunter,
Stephane Eranian, David Ahern, Vince Weaver, linux-kernel
On Wed, Jun 04, 2014 at 11:46:18PM +0200, Rickard Strandqvist wrote:
> Added a guaranteed null-terminate after call to strncpy.
>
> This was partly found using a static code analysis program called cppcheck.
Should just stop using strncpy. strncpy semantics don't make any
sense at all, and it's inefficient. I would just snprintf here
-Andi
>
> Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
> ---
> tools/perf/util/parse-events.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
> index 1e15df1..fad2976 100644
> --- a/tools/perf/util/parse-events.c
> +++ b/tools/perf/util/parse-events.c
> @@ -1216,8 +1216,10 @@ static void print_symbol_events(const char *event_glob, unsigned type,
>
> if (strlen(syms->alias))
> snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
> - else
> + else {
> strncpy(name, syms->symbol, MAX_NAME_LEN);
> + name[sizeof(name) - 1] = '\0';
> + }
>
> printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
>
> --
> 1.7.10.4
>
--
ak@linux.intel.com -- Speaking for myself only
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] tools: perf: util: parse-events.c: Cleaning up missing null-terminate after strncpy call
2014-06-04 22:20 ` Andi Kleen
@ 2014-06-04 22:23 ` Rickard Strandqvist
2014-06-04 22:31 ` Andi Kleen
0 siblings, 1 reply; 6+ messages in thread
From: Rickard Strandqvist @ 2014-06-04 22:23 UTC (permalink / raw)
To: Andi Kleen
Cc: Peter Zijlstra, Paul Mackerras, Ingo Molnar,
Arnaldo Carvalho de Melo, Jiri Olsa, Namhyung Kim, Adrian Hunter,
Stephane Eranian, David Ahern, Vince Weaver,
linux-kernel@vger.kernel.org
Hi
A little embarrassing, but I actually did not know that there was a
better replacement for strncpy.
Sorry, but I will send a new platch based on strlcpy instead then,
snprintf i slow and unnecessary in this case.
Best regards
Rickard Strandqvist
2014-06-05 0:20 GMT+02:00 Andi Kleen <ak@linux.intel.com>:
> On Wed, Jun 04, 2014 at 11:46:18PM +0200, Rickard Strandqvist wrote:
>> Added a guaranteed null-terminate after call to strncpy.
>>
>> This was partly found using a static code analysis program called cppcheck.
>
> Should just stop using strncpy. strncpy semantics don't make any
> sense at all, and it's inefficient. I would just snprintf here
>
> -Andi
>
>>
>> Signed-off-by: Rickard Strandqvist <rickard_strandqvist@spectrumdigital.se>
>> ---
>> tools/perf/util/parse-events.c | 4 +++-
>> 1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/tools/perf/util/parse-events.c b/tools/perf/util/parse-events.c
>> index 1e15df1..fad2976 100644
>> --- a/tools/perf/util/parse-events.c
>> +++ b/tools/perf/util/parse-events.c
>> @@ -1216,8 +1216,10 @@ static void print_symbol_events(const char *event_glob, unsigned type,
>>
>> if (strlen(syms->alias))
>> snprintf(name, MAX_NAME_LEN, "%s OR %s", syms->symbol, syms->alias);
>> - else
>> + else {
>> strncpy(name, syms->symbol, MAX_NAME_LEN);
>> + name[sizeof(name) - 1] = '\0';
>> + }
>>
>> printf(" %-50s [%s]\n", name, event_type_descriptors[type]);
>>
>> --
>> 1.7.10.4
>>
>
> --
> ak@linux.intel.com -- Speaking for myself only
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] tools: perf: util: parse-events.c: Cleaning up missing null-terminate after strncpy call
2014-06-04 22:23 ` Rickard Strandqvist
@ 2014-06-04 22:31 ` Andi Kleen
2014-06-06 13:16 ` Rickard Strandqvist
0 siblings, 1 reply; 6+ messages in thread
From: Andi Kleen @ 2014-06-04 22:31 UTC (permalink / raw)
To: Rickard Strandqvist
Cc: Peter Zijlstra, Paul Mackerras, Ingo Molnar,
Arnaldo Carvalho de Melo, Jiri Olsa, Namhyung Kim, Adrian Hunter,
Stephane Eranian, David Ahern, Vince Weaver,
linux-kernel@vger.kernel.org
On Thu, Jun 05, 2014 at 12:23:55AM +0200, Rickard Strandqvist wrote:
> Hi
>
> A little embarrassing, but I actually did not know that there was a
> better replacement for strncpy.
This works for perf, but not in general because standard glibc
does not have strlcpy. snprintf works always.
In practice if you could tolerate strncpy always zeroing
the complete string before you can also tolerate snprintf.
-Andi
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] tools: perf: util: parse-events.c: Cleaning up missing null-terminate after strncpy call
2014-06-04 22:31 ` Andi Kleen
@ 2014-06-06 13:16 ` Rickard Strandqvist
2014-06-06 13:22 ` Jiri Olsa
0 siblings, 1 reply; 6+ messages in thread
From: Rickard Strandqvist @ 2014-06-06 13:16 UTC (permalink / raw)
To: Andi Kleen
Cc: Peter Zijlstra, Paul Mackerras, Ingo Molnar,
Arnaldo Carvalho de Melo, Jiri Olsa, Namhyung Kim, Adrian Hunter,
Stephane Eranian, David Ahern, Vince Weaver,
linux-kernel@vger.kernel.org
Hi
I really do not want to complicate things here, both work course here.
But sptlcpy is definitely faster. But this is hardly time-critical
code, so maybe it looks better to use snprintf in both cases.
I vote for sptlcpy, but who decides?
Someone decides and I submit another patch :-)
Best regards
Rickard Strandqvist
2014-06-05 0:31 GMT+02:00 Andi Kleen <ak@linux.intel.com>:
> On Thu, Jun 05, 2014 at 12:23:55AM +0200, Rickard Strandqvist wrote:
>> Hi
>>
>> A little embarrassing, but I actually did not know that there was a
>> better replacement for strncpy.
>
> This works for perf, but not in general because standard glibc
> does not have strlcpy. snprintf works always.
>
> In practice if you could tolerate strncpy always zeroing
> the complete string before you can also tolerate snprintf.
>
> -Andi
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] tools: perf: util: parse-events.c: Cleaning up missing null-terminate after strncpy call
2014-06-06 13:16 ` Rickard Strandqvist
@ 2014-06-06 13:22 ` Jiri Olsa
0 siblings, 0 replies; 6+ messages in thread
From: Jiri Olsa @ 2014-06-06 13:22 UTC (permalink / raw)
To: Rickard Strandqvist
Cc: Andi Kleen, Peter Zijlstra, Paul Mackerras, Ingo Molnar,
Arnaldo Carvalho de Melo, Namhyung Kim, Adrian Hunter,
Stephane Eranian, David Ahern, Vince Weaver,
linux-kernel@vger.kernel.org
On Fri, Jun 06, 2014 at 03:16:56PM +0200, Rickard Strandqvist wrote:
> Hi
>
> I really do not want to complicate things here, both work course here.
> But sptlcpy is definitely faster. But this is hardly time-critical
> code, so maybe it looks better to use snprintf in both cases.
>
> I vote for sptlcpy, but who decides?
> Someone decides and I submit another patch :-)
I vote for snprintf ;-)
jirka
>
> Best regards
> Rickard Strandqvist
>
>
> 2014-06-05 0:31 GMT+02:00 Andi Kleen <ak@linux.intel.com>:
> > On Thu, Jun 05, 2014 at 12:23:55AM +0200, Rickard Strandqvist wrote:
> >> Hi
> >>
> >> A little embarrassing, but I actually did not know that there was a
> >> better replacement for strncpy.
> >
> > This works for perf, but not in general because standard glibc
> > does not have strlcpy. snprintf works always.
> >
> > In practice if you could tolerate strncpy always zeroing
> > the complete string before you can also tolerate snprintf.
> >
> > -Andi
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2014-06-06 13:23 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-06-04 21:46 [PATCH] tools: perf: util: parse-events.c: Cleaning up missing null-terminate after strncpy call Rickard Strandqvist
2014-06-04 22:20 ` Andi Kleen
2014-06-04 22:23 ` Rickard Strandqvist
2014-06-04 22:31 ` Andi Kleen
2014-06-06 13:16 ` Rickard Strandqvist
2014-06-06 13:22 ` Jiri Olsa
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.