Fix: error-query: leak of trigger on allocation error
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 21 Apr 2021 18:39:52 +0000 (14:39 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 21 Apr 2021 18:55:35 +0000 (14:55 -0400)
1452630 Resource leak

The system resource will not be reclaimed and reused, reducing the
future availability of the resource.

In lttng_error_query_action_create: Leak of memory or pointers to system
resources (CWE-404)

CID 1452630 (#1 of 1): Resource leak (RESOURCE_LEAK)
21. leaked_storage: Variable trigger_copy going out of scope leaks the
storage it points to.

Reported-by: Coverity Scan
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: I4dd99390f1fd5ad957ab6500971dd2ed67a1c722

src/common/error-query.c

index d353c5a8ff090e5064114facb3c9d44e031ffe6f..d4395b467f265a8fda8f8e461692aeec78bb9ef2 100644 (file)
@@ -95,25 +95,29 @@ struct lttng_error_query *lttng_error_query_trigger_create(
                const struct lttng_trigger *trigger)
 {
        struct lttng_error_query_trigger *query = NULL;
-       struct lttng_trigger *trigger_copy;
+       struct lttng_trigger *trigger_copy = NULL;
 
-       trigger_copy = lttng_trigger_copy(trigger);
-       if (!trigger_copy) {
+       if (!trigger) {
                goto end;
        }
 
-       if (!trigger) {
+       trigger_copy = lttng_trigger_copy(trigger);
+       if (!trigger_copy) {
                goto end;
        }
 
        query = zmalloc(sizeof(*query));
        if (!query) {
                PERROR("Failed to allocate trigger error query");
-               goto end;
+               goto error;
        }
 
        query->parent.target_type = LTTNG_ERROR_QUERY_TARGET_TYPE_TRIGGER;
        query->trigger = trigger_copy;
+       trigger_copy = NULL;
+
+error:
+       lttng_trigger_put(trigger_copy);
 end:
        return query ? &query->parent : NULL;
 }
@@ -124,7 +128,7 @@ extern struct lttng_error_query *lttng_error_query_action_create(
 {
        struct lttng_error_query_action *query = NULL;
        typeof(query->action_index) action_index;
-       struct lttng_trigger *trigger_copy;
+       struct lttng_trigger *trigger_copy = NULL;
 
        if (!trigger || !action) {
                goto end;
@@ -153,7 +157,7 @@ extern struct lttng_error_query *lttng_error_query_action_create(
                action_status = lttng_action_group_get_count(
                                trigger->action, &action_group_count);
                if (action_status != LTTNG_ACTION_STATUS_OK) {
-                       goto end;
+                       goto error;
                }
 
                for (i = 0; i < action_group_count; i++) {
@@ -170,25 +174,28 @@ extern struct lttng_error_query *lttng_error_query_action_create(
 
                if (!action_index.is_set) {
                        /* Not found; invalid action. */
-                       goto end;
+                       goto error;
                }
        } else {
                /*
                 * Trigger action is not a group and not equal to the target
                 * action; invalid action provided.
                 */
-               goto end;
+               goto error;
        }
 
        query = zmalloc(sizeof(*query));
        if (!query) {
                PERROR("Failed to allocate action error query");
-               goto end;
+               goto error;
        }
 
        query->parent.target_type = LTTNG_ERROR_QUERY_TARGET_TYPE_ACTION;
        query->trigger = trigger_copy;
+       trigger_copy = NULL;
        query->action_index = action_index;
+error:
+       lttng_trigger_put(trigger_copy);
 end:
        return query ? &query->parent : NULL;
 }
This page took 0.026662 seconds and 4 git commands to generate.