linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* perf: Use strcmp(str, "const") instead of strncmp(str, "const", sizeof("const"))
@ 2018-12-20 17:26 Steven Rostedt
  2018-12-20 18:55 ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 3+ messages in thread
From: Steven Rostedt @ 2018-12-20 17:26 UTC (permalink / raw)
  To: LKML
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Jiri Olsa, Namhyung Kim,
	Alexander Shishkin

As strncmp(str, "const", sizeof("const") is exactly the same as
strcmp(str, "const") use that instead, otherwise it is confusing.

sizeof("const") includes the nul terminator ('\0') of the string
"const", and that means strncmp() will only return a match if str and
"const" are exactly the same, which is what strcmp() does.

Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
---
diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
index 32ef7bdca1cf..eabf30f963a5 100644
--- a/tools/perf/util/callchain.c
+++ b/tools/perf/util/callchain.c
@@ -258,7 +258,7 @@ int parse_callchain_record(const char *arg, struct callchain_param *param)
 
 	do {
 		/* Framepointer style */
-		if (!strncmp(name, "fp", sizeof("fp"))) {
+		if (!strcmp(name, "fp")) {
 			if (!strtok_r(NULL, ",", &saveptr)) {
 				param->record_mode = CALLCHAIN_FP;
 				ret = 0;
@@ -268,7 +268,7 @@ int parse_callchain_record(const char *arg, struct callchain_param *param)
 			break;
 
 		/* Dwarf style */
-		} else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
+		} else if (!strcmp(name, "dwarf")) {
 			const unsigned long default_stack_dump_size = 8192;
 
 			ret = 0;

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

* Re: perf: Use strcmp(str, "const") instead of strncmp(str, "const", sizeof("const"))
  2018-12-20 17:26 perf: Use strcmp(str, "const") instead of strncmp(str, "const", sizeof("const")) Steven Rostedt
@ 2018-12-20 18:55 ` Arnaldo Carvalho de Melo
  2019-01-01 18:00   ` Jiri Olsa
  0 siblings, 1 reply; 3+ messages in thread
From: Arnaldo Carvalho de Melo @ 2018-12-20 18:55 UTC (permalink / raw)
  To: Steven Rostedt
  Cc: LKML, Ingo Molnar, Jiri Olsa, Namhyung Kim, Alexander Shishkin

Em Thu, Dec 20, 2018 at 12:26:01PM -0500, Steven Rostedt escreveu:
> As strncmp(str, "const", sizeof("const") is exactly the same as
> strcmp(str, "const") use that instead, otherwise it is confusing.
> 
> sizeof("const") includes the nul terminator ('\0') of the string
> "const", and that means strncmp() will only return a match if str and
> "const" are exactly the same, which is what strcmp() does.

There are more of those, that are there from time immemorial, lemme see
if the original intention can be found... 

commit 26d330226b9cf6208daae9b0b3697980c8fb51d8
Author: Jiri Olsa <jolsa@redhat.com>
Date:   Tue Aug 7 15:20:47 2012 +0200

    perf tools: Support for DWARF mode callchain

----------------

I thought this could be because at the time strchr was used and thus the
name would be in a buffer followed by ',' or other separator, but
strtok_r() was used, so your patch should simplify things.

- Arnaldo
 
> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
> ---
> diff --git a/tools/perf/util/callchain.c b/tools/perf/util/callchain.c
> index 32ef7bdca1cf..eabf30f963a5 100644
> --- a/tools/perf/util/callchain.c
> +++ b/tools/perf/util/callchain.c
> @@ -258,7 +258,7 @@ int parse_callchain_record(const char *arg, struct callchain_param *param)
>  
>  	do {
>  		/* Framepointer style */
> -		if (!strncmp(name, "fp", sizeof("fp"))) {
> +		if (!strcmp(name, "fp")) {
>  			if (!strtok_r(NULL, ",", &saveptr)) {
>  				param->record_mode = CALLCHAIN_FP;
>  				ret = 0;
> @@ -268,7 +268,7 @@ int parse_callchain_record(const char *arg, struct callchain_param *param)
>  			break;
>  
>  		/* Dwarf style */
> -		} else if (!strncmp(name, "dwarf", sizeof("dwarf"))) {
> +		} else if (!strcmp(name, "dwarf")) {
>  			const unsigned long default_stack_dump_size = 8192;
>  
>  			ret = 0;

-- 

- Arnaldo

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

* Re: perf: Use strcmp(str, "const") instead of strncmp(str, "const", sizeof("const"))
  2018-12-20 18:55 ` Arnaldo Carvalho de Melo
@ 2019-01-01 18:00   ` Jiri Olsa
  0 siblings, 0 replies; 3+ messages in thread
From: Jiri Olsa @ 2019-01-01 18:00 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Steven Rostedt, LKML, Ingo Molnar, Namhyung Kim, Alexander Shishkin

On Thu, Dec 20, 2018 at 03:55:53PM -0300, Arnaldo Carvalho de Melo wrote:
> Em Thu, Dec 20, 2018 at 12:26:01PM -0500, Steven Rostedt escreveu:
> > As strncmp(str, "const", sizeof("const") is exactly the same as
> > strcmp(str, "const") use that instead, otherwise it is confusing.
> > 
> > sizeof("const") includes the nul terminator ('\0') of the string
> > "const", and that means strncmp() will only return a match if str and
> > "const" are exactly the same, which is what strcmp() does.
> 
> There are more of those, that are there from time immemorial, lemme see
> if the original intention can be found... 
> 
> commit 26d330226b9cf6208daae9b0b3697980c8fb51d8
> Author: Jiri Olsa <jolsa@redhat.com>
> Date:   Tue Aug 7 15:20:47 2012 +0200
> 
>     perf tools: Support for DWARF mode callchain
> 
> ----------------
> 
> I thought this could be because at the time strchr was used and thus the
> name would be in a buffer followed by ',' or other separator, but
> strtok_r() was used, so your patch should simplify things.

yea, strtok_r returns null-terminated string containing the next token,
so there's always trailing null terminator.. so those strncmp calls
work properly

jirka

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

end of thread, other threads:[~2019-01-01 18:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-20 17:26 perf: Use strcmp(str, "const") instead of strncmp(str, "const", sizeof("const")) Steven Rostedt
2018-12-20 18:55 ` Arnaldo Carvalho de Melo
2019-01-01 18:00   ` Jiri Olsa

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