From 3dfbc05c534e498e2196a0f6ecd8c64a9da1115d Mon Sep 17 00:00:00 2001
From: Corey Farrell <git@cfware.com>
Date: Thu, 19 Sep 2019 18:32:56 -0400
Subject: [PATCH] stasis: Fix leaks

* Release reference returned by cache_remove
* state_alloc unconditionally bumped state_topic even when it was
  locally allocated.

Change-Id: I51101bf7d07b8dc8ce8fc46b6cb31fbbd213fbc7
---
 main/stasis_cache.c |  2 +-
 main/stasis_state.c | 14 +++++++-------
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/main/stasis_cache.c b/main/stasis_cache.c
index 6be4bf1916..bcd7e7d1ff 100644
--- a/main/stasis_cache.c
+++ b/main/stasis_cache.c
@@ -868,7 +868,7 @@ static void caching_topic_exec(void *data, struct stasis_subscription *sub,
 			ao2_wrlock(caching_topic->cache->entries);
 			sub = cache_find(caching_topic->cache->entries, stasis_subscription_change_type(), change->uniqueid);
 			if (sub) {
-				cache_remove(caching_topic->cache->entries, sub, stasis_message_eid(message));
+				ao2_cleanup(cache_remove(caching_topic->cache->entries, sub, stasis_message_eid(message)));
 				ao2_cleanup(sub);
 			}
 			ao2_unlock(caching_topic->cache->entries);
diff --git a/main/stasis_state.c b/main/stasis_state.c
index a9159724d0..aa00f9a4d4 100644
--- a/main/stasis_state.c
+++ b/main/stasis_state.c
@@ -165,6 +165,13 @@ static struct stasis_state *state_alloc(struct stasis_state_manager *manager,
 			return NULL;
 		}
 		ast_free(name);
+	} else {
+		/*
+		 * Since the state topic was passed in, go ahead and bump its reference.
+		 * By doing this here first, it allows us to consistently decrease the reference on
+		 * state allocation error.
+		 */
+		ao2_ref(state_topic, +1);
 	}
 
 	if (!id) {
@@ -175,13 +182,6 @@ static struct stasis_state *state_alloc(struct stasis_state_manager *manager,
 		id = state_id_by_topic(manager->all_topic, state_topic);
 	}
 
-	/*
-	 * Since the state topic could have been passed in, go ahead and bump its reference.
-	 * By doing this here first, it allows us to consistently decrease the reference on
-	 * state allocation error.
-	 */
-	ao2_ref(state_topic, +1);
-
 	state = ao2_alloc(sizeof(*state) + strlen(id) + 1, state_dtor);
 	if (!state) {
 		ast_log(LOG_ERROR, "Unable to allocate state '%s' in manager '%s'\n",
-- 
GitLab