From: Jérémie Galarneau Date: Wed, 21 Apr 2021 18:39:52 +0000 (-0400) Subject: Fix: error-query: leak of trigger on allocation error X-Git-Tag: v2.13.0-rc1~60 X-Git-Url: https://git.lttng.org/?a=commitdiff_plain;h=3746c2785d827563ca95bb36b649837d2811cc8d;hp=82e2adac0e195e560ac607f22bf67db267b6d464;p=lttng-tools.git Fix: error-query: leak of trigger on allocation error 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 --- 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; }