lib: add null checks before "casting" to "base class"
authorSimon Marchi <simon.marchi@efficios.com>
Sun, 22 Jan 2023 19:35:32 +0000 (14:35 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 7 Mar 2023 16:23:40 +0000 (11:23 -0500)
With -fsanitize=undefined, I see:

    /home/simark/src/babeltrace/src/lib/error.c:526:2: runtime error: member access within null pointer of type 'struct bt_error_cause_component_actor'

This is because doing `&cause->base` is undefined behavior if cause is
NULL.  Add NULL checks around these expressions, when cause may be NULL.

Change-Id: Ie11273a24aa17a96bfccf10b121838e48c61984b
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/9584
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/lib/error.c

index 9144bd96ddc528c9f52e47afe88d0c195bd678ed..bd8259ab71057103385d75b711ffd59a5508007a 100644 (file)
@@ -300,8 +300,10 @@ struct bt_error_cause_component_actor *create_error_cause_component_actor(
        goto end;
 
 error:
-       destroy_error_cause(&cause->base);
-       cause = NULL;
+       if (cause) {
+               destroy_error_cause(&cause->base);
+               cause = NULL;
+       }
 
 end:
        return cause;
@@ -341,8 +343,10 @@ create_error_cause_component_class_actor(struct bt_component_class *comp_cls,
        goto end;
 
 error:
-       destroy_error_cause(&cause->base);
-       cause = NULL;
+       if (cause) {
+               destroy_error_cause(&cause->base);
+               cause = NULL;
+       }
 
 end:
        return cause;
@@ -407,8 +411,10 @@ create_error_cause_message_iterator_actor(struct bt_message_iterator *iter,
        goto end;
 
 error:
-       destroy_error_cause(&cause->base);
-       cause = NULL;
+       if (cause) {
+               destroy_error_cause(&cause->base);
+               cause = NULL;
+       }
 
 end:
        return cause;
@@ -523,7 +529,10 @@ int bt_error_append_cause_from_component(
        cause = NULL;
 
 end:
-       destroy_error_cause(&cause->base);
+       if (cause) {
+               destroy_error_cause(&cause->base);
+       }
+
        return status;
 }
 
@@ -557,7 +566,10 @@ int bt_error_append_cause_from_component_class(
        cause = NULL;
 
 end:
-       destroy_error_cause(&cause->base);
+       if (cause) {
+               destroy_error_cause(&cause->base);
+       }
+
        return status;
 }
 
@@ -590,7 +602,10 @@ int bt_error_append_cause_from_message_iterator(
        cause = NULL;
 
 end:
-       destroy_error_cause(&cause->base);
+       if (cause) {
+               destroy_error_cause(&cause->base);
+       }
+
        return status;
 }
 
This page took 0.026167 seconds and 4 git commands to generate.