Commit d22bbe1ab004734994b0b4048e0ce3ce80856188

Maksym Borodin 2013-11-13T11:42:33

api.c: Release apisock on error in api()

diff --git a/api.c b/api.c
index f9e2fd0..420f04d 100644
--- a/api.c
+++ b/api.c
@@ -4930,6 +4930,7 @@ void api(int api_thr_id)
 
 	if (!opt_api_listen) {
 		applog(LOG_DEBUG, "API not running%s", UNAVAILABLE);
+		free(apisock);
 		return;
 	}
 
@@ -4947,6 +4948,7 @@ void api(int api_thr_id)
 
 		if (ips == 0) {
 			applog(LOG_WARNING, "API not running (no valid IPs specified)%s", UNAVAILABLE);
+			free(apisock);
 			return;
 		}
 	}
@@ -4958,6 +4960,7 @@ void api(int api_thr_id)
 	*apisock = socket(AF_INET, SOCK_STREAM, 0);
 	if (*apisock == INVSOCK) {
 		applog(LOG_ERR, "API1 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
+		free(apisock);
 		return;
 	}
 
@@ -4969,6 +4972,7 @@ void api(int api_thr_id)
 		serv.sin_addr.s_addr = inet_addr(localaddr);
 		if (serv.sin_addr.s_addr == (in_addr_t)INVINETADDR) {
 			applog(LOG_ERR, "API2 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
+			free(apisock);
 			return;
 		}
 	}
@@ -5007,12 +5011,14 @@ void api(int api_thr_id)
 
 	if (bound == 0) {
 		applog(LOG_ERR, "API bind to port %d failed (%s)%s", port, binderror, UNAVAILABLE);
+		free(apisock);
 		return;
 	}
 
 	if (SOCKETFAIL(listen(*apisock, QUEUE))) {
 		applog(LOG_ERR, "API3 initialisation failed (%s)%s", SOCKERRMSG, UNAVAILABLE);
 		CLOSESOCKET(*apisock);
+		free(apisock);
 		return;
 	}
 
@@ -5149,6 +5155,8 @@ die:
 	;
 	pthread_cleanup_pop(true);
 
+	free(apisock);
+	
 	if (opt_debug)
 		applog(LOG_DEBUG, "API: terminating due to: %s",
 				do_a_quit ? "QUIT" : (do_a_restart ? "RESTART" : (bye ? "BYE" : "UNKNOWN!")));