From 2c31cfe3b3318a535ec9a1afcf15f632c9f8ef79 Mon Sep 17 00:00:00 2001
From: Matthew Jordan <mjordan@digium.com>
Date: Thu, 6 Dec 2012 14:11:21 +0000
Subject: [PATCH] Fix memory leak in 'manager show event' when command entered
 incorrectly

When the CLI command 'manager show event' was run incorrectly and its usage
instructions returned, a reference to the event container was leaked. This
would prevent the container from being reclaimed when Asterisk exits. We now
properly decrement the count on the ao2 object using the nifty RAII_VAR macro.

Thanks to Russell for helping me stumble on this, and Terry for writing that
ridiculously helpful macro.


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/11@377319 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 main/manager.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/main/manager.c b/main/manager.c
index c24610a38c..32991e21c8 100644
--- a/main/manager.c
+++ b/main/manager.c
@@ -7117,7 +7117,7 @@ static char *handle_manager_show_events(struct ast_cli_entry *e, int cmd, struct
 
 static char *handle_manager_show_event(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
 {
-	struct ao2_container *events;
+	RAII_VAR(struct ao2_container *, events, NULL, ao2_cleanup);
 	struct ao2_iterator it_events;
 	struct ast_xml_doc_item *item, *temp;
 	int length;
@@ -7152,7 +7152,6 @@ static char *handle_manager_show_event(struct ast_cli_entry *e, int cmd, struct
 			ao2_ref(item, -1);
 		}
 		ao2_iterator_destroy(&it_events);
-		ao2_ref(events, -1);
 		return match;
 	}
 
@@ -7162,7 +7161,6 @@ static char *handle_manager_show_event(struct ast_cli_entry *e, int cmd, struct
 
 	if (!(item = ao2_find(events, a->argv[3], OBJ_KEY))) {
 		ast_cli(a->fd, "Could not find event '%s'\n", a->argv[3]);
-		ao2_ref(events, -1);
 		return CLI_SUCCESS;
 	}
 
@@ -7202,7 +7200,6 @@ static char *handle_manager_show_event(struct ast_cli_entry *e, int cmd, struct
 	}
 
 	ao2_ref(item, -1);
-	ao2_ref(events, -1);
 	return CLI_SUCCESS;
 }
 
-- 
GitLab