From 3746c2785d827563ca95bb36b649837d2811cc8d Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Wed, 21 Apr 2021 14:39:52 -0400 Subject: [PATCH] Fix: error-query: leak of trigger on allocation error MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 Change-Id: I4dd99390f1fd5ad957ab6500971dd2ed67a1c722 --- src/common/error-query.c | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/common/error-query.c b/src/common/error-query.c index d353c5a8f..d4395b467 100644 --- a/src/common/error-query.c +++ b/src/common/error-query.c @@ -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; } -- 2.34.1