Skip to contentSkip to conversation

perf: Use strcmp(str, "const") instead of strncmp(str, "const", sizeof("const"))

PATCH Steven Rostedt 3 participants 3 messages Dec 20, 2018 – Jan 1, 2019 perf events

lore
Share & tools
lore
#0 lore raw

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.

---

tools/perf/util/callchain.c +2 -2
index 32ef7bdca1cf..eabf30f963a5 100644
@@ -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;
#1 lore raw

Em Thu, Dec 20, 2018 at 12:26:01PM -0500, Steven Rostedt escreveu:

Quoted 6 lines

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 <[email protected]>
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

Quoted 24 lines

---

tools/perf/util/callchain.c +2 -2
index 32ef7bdca1cf..eabf30f963a5 100644
@@ -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;
Signature · 1 line
- Arnaldo
#2 lore raw
Quoted 15 lines · Arnaldo Carvalho de Melo

Em Thu, Dec 20, 2018 at 12:26:01PM -0500, Steven Rostedt escreveu:

Quoted 6 lines

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 <[email protected]>
Date:   Tue Aug 7 15:20:47 2012 +0200
Quoted 7 lines

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