Skip to contentSkip to conversation

tools lib traceevent: Fix missing equality check for strcmp

PATCH In review Rikard Falkeborn 4 participants 5 messages Apr 9, 2019 – Apr 16, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools lib traceevent: Fix missing equality check for strcmp

There was a missing comparison with 0 when checking if type is "s64" or "u64". Therefore, the body of the if-statement was entered if "type" was "u64" or not "s64", which made the first strcmp() redundant since if type is "u64", it's not "s64".

If type is "s64", the body of the if-statement is not entered but since the remainder of the function consists of if-statements which will not be entered if type is "s64", we will just return "val", which is correct, albeit at the cost of a few more calls to strcmp(), i.e., it will behave just as if the if-statement was entered.

If type is neither "s64" or "u64", the body of the if-statement will be entered incorrectly and "val" returned. This means that any type that is checked after "s64" and "u64" is handled the same way as "s64" and "u64", i.e., the limiting of "val" to fit in for example "s8" is never reached.

This was introduced in the kernel tree when the sources were copied from trace-cmd in commit f7d82350e597 ("tools/events: Add files to create libtraceevent.a"), and in the trace-cmd repo in 1cdbae6035cei ("Implement typecasting in parser") when the function was introduced, i.e., it has always behaved the wrong way.

Detected by cppcheck.

Fixes f7d82350e597 ("tools/events: Add files to create libtraceevent.a")

---
I have only compile tested the patch but it should be correct. I don't know if any other problems will surface due to this though.

---
 tools/lib/traceevent/event-parse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/traceevent/event-parse.c +1 -1
index 87494c7c619d..981c6ce2da2c 100644
@@ -2233,7 +2233,7 @@ eval_type_str(unsigned long long val, const char *type, int pointer)
 		return val & 0xffffffff;
 
 	if (strcmp(type, "u64") == 0 ||
-	    strcmp(type, "s64"))
+	    strcmp(type, "s64") == 0)
 		return val;
 
 	if (strcmp(type, "s8") == 0)
Signature · 1 line
2.21.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Rikard,

Quoted 17 lines · On Tue, 9 Apr 2019 11:15:29 +0200 Rikard Falkeborn…

There was a missing comparison with 0 when checking if type is "s64" or "u64". Therefore, the body of the if-statement was entered if "type" was "u64" or not "s64", which made the first strcmp() redundant since if type is "u64", it's not "s64".

If type is "s64", the body of the if-statement is not entered but since the remainder of the function consists of if-statements which will not be entered if type is "s64", we will just return "val", which is correct, albeit at the cost of a few more calls to strcmp(), i.e., it will behave just as if the if-statement was entered.

If type is neither "s64" or "u64", the body of the if-statement will be entered incorrectly and "val" returned. This means that any type that is checked after "s64" and "u64" is handled the same way as "s64" and "u64", i.e., the limiting of "val" to fit in for example "s8" is never reached.

Quoted 7 lines

This was introduced in the kernel tree when the sources were copied from trace-cmd in commit f7d82350e597 ("tools/events: Add files to create libtraceevent.a"), and in the trace-cmd repo in 1cdbae6035cei ("Implement typecasting in parser") when the function was introduced, i.e., it has always behaved the wrong way.

Detected by cppcheck.

Nice. Thanks for this.

Quoted 3 lines
Fixes f7d82350e597 ("tools/events: Add files to create libtraceevent.a")

Arnaldo,

Care to pull this in?

Thanks!

-- Steve

Quoted 20 lines

---
I have only compile tested the patch but it should be correct. I don't know if any other problems will surface due to this though.

---
 tools/lib/traceevent/event-parse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/traceevent/event-parse.c +1 -1
index 87494c7c619d..981c6ce2da2c 100644
@@ -2233,7 +2233,7 @@ eval_type_str(unsigned long long val, const char *type, int pointer)
 		return val & 0xffffffff;
 
 	if (strcmp(type, "u64") == 0 ||
-	    strcmp(type, "s64"))
+	    strcmp(type, "s64") == 0)
 		return val;
 
 	if (strcmp(type, "s8") == 0)
#2 lore raw

Em Tue, Apr 09, 2019 at 09:21:22AM -0400, Steven Rostedt escreveu:

Quoted 34 lines

Hi Rikard,

Quoted 17 lines · On Tue, 9 Apr 2019 11:15:29 +0200 Rikard Falkeborn…

There was a missing comparison with 0 when checking if type is "s64" or "u64". Therefore, the body of the if-statement was entered if "type" was "u64" or not "s64", which made the first strcmp() redundant since if type is "u64", it's not "s64".

If type is "s64", the body of the if-statement is not entered but since the remainder of the function consists of if-statements which will not be entered if type is "s64", we will just return "val", which is correct, albeit at the cost of a few more calls to strcmp(), i.e., it will behave just as if the if-statement was entered.

If type is neither "s64" or "u64", the body of the if-statement will be entered incorrectly and "val" returned. This means that any type that is checked after "s64" and "u64" is handled the same way as "s64" and "u64", i.e., the limiting of "val" to fit in for example "s8" is never reached.

Quoted 7 lines

This was introduced in the kernel tree when the sources were copied from trace-cmd in commit f7d82350e597 ("tools/events: Add files to create libtraceevent.a"), and in the trace-cmd repo in 1cdbae6035cei ("Implement typecasting in parser") when the function was introduced, i.e., it has always behaved the wrong way.

Detected by cppcheck.

Nice. Thanks for this.

Quoted 1 line
Quoted 8 lines
Quoted 2 lines
Fixes f7d82350e597 ("tools/events: Add files to create libtraceevent.a")

Arnaldo,

Care to pull this in?

Sure, thanks.

- Arnaldo

Quoted 24 lines

Thanks!

-- Steve

Quoted 20 lines

---
I have only compile tested the patch but it should be correct. I don't know if any other problems will surface due to this though.

---
 tools/lib/traceevent/event-parse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/traceevent/event-parse.c +1 -1
index 87494c7c619d..981c6ce2da2c 100644
@@ -2233,7 +2233,7 @@ eval_type_str(unsigned long long val, const char *type, int pointer)
 		return val & 0xffffffff;
 
 	if (strcmp(type, "u64") == 0 ||
-	    strcmp(type, "s64"))
+	    strcmp(type, "s64") == 0)
 		return val;
 
 	if (strcmp(type, "s8") == 0)
#3 lore raw
In reply to Rikard Falkeborn
tools lib traceevent: Fix missing equality check for strcmp
Commit-ID:  ff4f5471801f879aea8f9e3ace2b9af31efc9178
Gitweb:     https://git.kernel.org/tip/ff4f5471801f879aea8f9e3ace2b9af31efc9178
Author:     Rikard Falkeborn <[email protected]>
AuthorDate: Tue, 9 Apr 2019 11:15:29 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 9 Apr 2019 11:55:30 -0300

tools lib traceevent: Fix missing equality check for strcmp

There was a missing comparison with 0 when checking if type is "s64" or "u64". Therefore, the body of the if-statement was entered if "type" was "u64" or not "s64", which made the first strcmp() redundant since if type is "u64", it's not "s64".

If type is "s64", the body of the if-statement is not entered but since the remainder of the function consists of if-statements which will not be entered if type is "s64", we will just return "val", which is correct, albeit at the cost of a few more calls to strcmp(), i.e., it will behave just as if the if-statement was entered.

If type is neither "s64" or "u64", the body of the if-statement will be entered incorrectly and "val" returned. This means that any type that is checked after "s64" and "u64" is handled the same way as "s64" and "u64", i.e., the limiting of "val" to fit in for example "s8" is never reached.

This was introduced in the kernel tree when the sources were copied from trace-cmd in commit f7d82350e597 ("tools/events: Add files to create libtraceevent.a"), and in the trace-cmd repo in 1cdbae6035cei ("Implement typecasting in parser") when the function was introduced, i.e., it has always behaved the wrong way.

Detected by cppcheck.

Cc Tzvetomir Stoyanov <[email protected]>
Fixes f7d82350e597 ("tools/events: Add files to create libtraceevent.a")
---
 tools/lib/traceevent/event-parse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/traceevent/event-parse.c +1 -1
index 87494c7c619d..981c6ce2da2c 100644
@@ -2233,7 +2233,7 @@ eval_type_str(unsigned long long val, const char *type, int pointer)
 		return val & 0xffffffff;
 
 	if (strcmp(type, "u64") == 0 ||
-	    strcmp(type, "s64"))
+	    strcmp(type, "s64") == 0)
 		return val;
 
 	if (strcmp(type, "s8") == 0)
#4 lore raw
In reply to Rikard Falkeborn
tools lib traceevent: Fix missing equality check for strcmp
Commit-ID:  f32c2877bcb068a718bb70094cd59ccc29d4d082
Gitweb:     https://git.kernel.org/tip/f32c2877bcb068a718bb70094cd59ccc29d4d082
Author:     Rikard Falkeborn <[email protected]>
AuthorDate: Tue, 9 Apr 2019 11:15:29 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Tue, 16 Apr 2019 11:27:36 -0300

tools lib traceevent: Fix missing equality check for strcmp

There was a missing comparison with 0 when checking if type is "s64" or "u64". Therefore, the body of the if-statement was entered if "type" was "u64" or not "s64", which made the first strcmp() redundant since if type is "u64", it's not "s64".

If type is "s64", the body of the if-statement is not entered but since the remainder of the function consists of if-statements which will not be entered if type is "s64", we will just return "val", which is correct, albeit at the cost of a few more calls to strcmp(), i.e., it will behave just as if the if-statement was entered.

If type is neither "s64" or "u64", the body of the if-statement will be entered incorrectly and "val" returned. This means that any type that is checked after "s64" and "u64" is handled the same way as "s64" and "u64", i.e., the limiting of "val" to fit in for example "s8" is never reached.

This was introduced in the kernel tree when the sources were copied from trace-cmd in commit f7d82350e597 ("tools/events: Add files to create libtraceevent.a"), and in the trace-cmd repo in 1cdbae6035cei ("Implement typecasting in parser") when the function was introduced, i.e., it has always behaved the wrong way.

Detected by cppcheck.

Cc Tzvetomir Stoyanov <[email protected]>
Fixes f7d82350e597 ("tools/events: Add files to create libtraceevent.a")
---
 tools/lib/traceevent/event-parse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/traceevent/event-parse.c +1 -1
index 87494c7c619d..981c6ce2da2c 100644
@@ -2233,7 +2233,7 @@ eval_type_str(unsigned long long val, const char *type, int pointer)
 		return val & 0xffffffff;
 
 	if (strcmp(type, "u64") == 0 ||
-	    strcmp(type, "s64"))
+	    strcmp(type, "s64") == 0)
 		return val;
 
 	if (strcmp(type, "s8") == 0)