From 4f68f39cde22f215e7570b996411e533bcde912d Mon Sep 17 00:00:00 2001
From: Richard Mudgett <rmudgett@digium.com>
Date: Tue, 17 Mar 2015 21:43:32 +0000
Subject: [PATCH] Audit ast_sockaddr_resolve() usage for memory leaks.

Valgrind found some memory leaks associated with ast_sockaddr_resolve().
Most of the leaks had already been fixed by earlier memory leak hunt
patches.  This patch performs an audit of ast_sockaddr_resolve() and found
one more.

* Fix ast_sockaddr_resolve() memory leak in
apps/app_externalivr.c:app_exec().

* Made main/netsock2.c:ast_sockaddr_resolve() always set the addrs
parameter for safety so the pointer will never be uninitialized on return.
The same goes for res/res_pjsip_acl.c:extract_contact_addr().

Review: https://reviewboard.asterisk.org/r/4509/


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/11@433056 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 apps/app_externalivr.c | 2 ++
 main/netsock2.c        | 4 ++++
 2 files changed, 6 insertions(+)

diff --git a/apps/app_externalivr.c b/apps/app_externalivr.c
index 0212e09d0e..765f5c9bd8 100644
--- a/apps/app_externalivr.c
+++ b/apps/app_externalivr.c
@@ -519,6 +519,8 @@ static int app_exec(struct ast_channel *chan, const char *data)
 			break;
 		}
 
+		ast_free(addrs);
+
 		if (i == num_addrs) {
 			ast_chan_log(LOG_ERROR, chan, "Could not connect to any host.  ExternalIVR failed.\n");
 			goto exit;
diff --git a/main/netsock2.c b/main/netsock2.c
index aa5e17d9e7..c53ad06aaa 100644
--- a/main/netsock2.c
+++ b/main/netsock2.c
@@ -253,11 +253,13 @@ int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
 	int	e, i, res_cnt;
 
 	if (!str) {
+		*addrs = NULL;
 		return 0;
 	}
 
 	s = ast_strdupa(str);
 	if (!ast_sockaddr_split_hostport(s, &host, &port, flags)) {
+		*addrs = NULL;
 		return 0;
 	}
 
@@ -268,6 +270,7 @@ int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
 	if ((e = getaddrinfo(host, port, &hints, &res))) {
 		ast_log(LOG_ERROR, "getaddrinfo(\"%s\", \"%s\", ...): %s\n",
 			host, S_OR(port, "(null)"), gai_strerror(e));
+		*addrs = NULL;
 		return 0;
 	}
 
@@ -277,6 +280,7 @@ int ast_sockaddr_resolve(struct ast_sockaddr **addrs, const char *str,
 	}
 
 	if (res_cnt == 0) {
+		*addrs = NULL;
 		goto cleanup;
 	}