From 568238b53927804119dcc892919654deb961c4eb Mon Sep 17 00:00:00 2001 From: GDR! Date: Tue, 7 Jul 2015 19:07:30 +0200 Subject: [PATCH] CID 122515 (#1 of 1): Resource leak (RESOURCE_LEAK)6. leaked_storage: Variable res going out of scope leaks the storage it points to. --- client.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/client.c b/client.c index 929e2f5..ace40bc 100644 --- a/client.c +++ b/client.c @@ -42,6 +42,7 @@ int local_bind() char port[6]; int yes = 1; int flags; + int gai_status; snprintf(port, 6, "%d", local_port); @@ -50,12 +51,18 @@ int local_bind() hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; // fill in my IP for me - getaddrinfo(NULL, port, &hints, &res); + gai_status = getaddrinfo(NULL, port, &hints, &res); + if(gai_status != 0) + { + log_printf(L_ERROR, "getaddrinfo: %s\n", gai_strerror(gai_status)); + exit(1); + } bind_sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); if(bind_sockfd < 0) { log_printf(L_ERROR, "Could not create a socket for local listening: %s\n", strerror(errno)); + freeaddrinfo(res); exit(1); } @@ -71,10 +78,13 @@ int local_bind() if(bind(bind_sockfd, res->ai_addr, res->ai_addrlen) < 0) { log_printf(L_ERROR, "Bind to port %d failed: %s\n", local_port, strerror(errno)); + freeaddrinfo(res); close(bind_sockfd); exit(1); } + freeaddrinfo(res); + if(listen(bind_sockfd, 1) < 0) { log_printf(L_ERROR, "Listening on port %d failed: %s\n", local_port, strerror(errno));