Skip to contentSkip to conversation

Fix several Coverity andf Clang warnings in libtraceevent

PATCH Applied Michael Petlan 2 participants 5 messages Sep 30, 2022 – Oct 20, 2022

lore
Share & tools
lore
#0 lore raw

cover Fix several Coverity andf Clang warnings in libtraceevent

Hello.

When rebasing to libtraceevent-1.5.3 in RHEL, Coverity has found some issues. I have tried to fix them. Could you please have a look at the patches attached?

Thank you.

Michael

Michael Petlan (3):
libtraceevent: Fix uninitialized has_0x compiler warning
libtraceevent: Fix check-after-deref coverity flaw
libtraceevent: Fix Branch condition garbage value compiler warning

 src/event-parse.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
Signature · 1 line
2.18.4
#1 lore raw

PATCH 1/3 libtraceevent: Fix uninitialized has_0x compiler warning

Initialize has_0x in order to supress the following compiler warning:

event-parse.c:5654:20: warning: 'has_0x' may be used uninitialized in this function [-Wmaybe-uninitialized]
5654 | if (has_0x)
| ^
event-parse.c:5619:14: note: 'has_0x' was declared here

   5619 |         bool has_0x;
        |              ^~~~~~
---
 src/event-parse.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/event-parse.c +1 -1
index 980e980..edf990a 100644
@@ -5657,7 +5657,7 @@ static inline void print_field(struct trace_seq *s, void *data, int size,
 	struct tep_print_parse *start_parse;
 	struct tep_print_parse *parse;
 	struct tep_print_arg *arg;
-	bool has_0x;
+	bool has_0x = false;
 
 	parse = parse_ptr ? *parse_ptr : event->print_fmt.print_cache;
 
Signature · 1 line
2.18.4
#2 lore raw

PATCH 2/3 libtraceevent: Fix check-after-deref coverity flaw

In reply to Michael Petlan

Before patch, both arg->bitmask.field and arg->string.field were checked for being NULL and if yes, some value was assigned to them. The value was immediately used (dereferenced) and after that, another check for NULL was performed (the one leading to break command). However, in case this check would be true, the dereferencing before would have already caused a crash.

Move the NULL checks before dereferencing the pointers.

---
 src/event-parse.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
src/event-parse.c +4 -4
index edf990a..b4094ec 100644
@@ -4623,10 +4623,10 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,
 	case TEP_PRINT_STRING: {
 		if (!arg->string.field) {
 			arg->string.field = tep_find_any_field(event, arg->string.string);
+			if (!arg->string.field)
+				break;
 			arg->string.offset = arg->string.field->offset;
 		}
-		if (!arg->string.field)
-			break;
 		dynamic_offset_field(tep, arg->string.field, data, size, &offset, &len);
 		/* Do not attempt to save zero length dynamic strings */
 		if (!len)
@@ -4640,10 +4640,10 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,
 	case TEP_PRINT_BITMASK: {
 		if (!arg->bitmask.field) {
 			arg->bitmask.field = tep_find_any_field(event, arg->bitmask.bitmask);
+			if (!arg->bitmask.field)
+				break;
 			arg->bitmask.offset = arg->bitmask.field->offset;
 		}
-		if (!arg->bitmask.field)
-			break;
 		dynamic_offset_field(tep, arg->bitmask.field, data, size, &offset, &len);
 		print_bitmask_to_seq(tep, s, format, len_arg,
 				     data + offset, len);
Signature · 1 line
2.18.4
#3 lore raw

PATCH 3/3 libtraceevent: Fix Branch condition garbage value compiler warning

In reply to Michael Petlan

If *offset equals to zero, it is zero. If not equals to zero, set it to zero. In any case, it will be zero, so we can omit the condition and so get rid of the compiler warning:

libtraceevent/src/event-parse.c:4064:7: warning[core.uninitialized.Branch]: Branch condition evaluates to a garbage value

Instead, let's rather check the pointers for being NULL, in order to prevent another warning:

libtraceevent/src/event-parse.c:4064:7: warning[core.NullDereference]: Dereference of null pointer (loaded from variable 'offset')

---
 src/event-parse.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/event-parse.c +2 -2
index b4094ec..60bf989 100644
@@ -4073,9 +4073,9 @@ static inline void dynamic_offset_field(struct tep_handle *tep,
 {
 	/* Test for overflow */
 	if (field->offset + field->size > size) {
-		if (*offset)
+		if (offset)
 			*offset = 0;
-		if (*len)
+		if (len)
 			*len = 0;
 		return;
 	}
Signature · 1 line
2.18.4
#4 lore raw
In reply to Michael Petlan
Quoted 7 lines · On Fri, 30 Sep 2022 13:09:59 +0200 Michael Petlan <[email protected]> wrote:

Hello.

When rebasing to libtraceevent-1.5.3 in RHEL, Coverity has found some issues. I have tried to fix them. Could you please have a look at the patches attached?

Thank you.

Applied. Thanks Michael!

-- Steve