From 781bd247805ff75f568d0224190580a762f44074 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Sat, 14 Feb 2015 07:11:38 +0100 Subject: Fix resource leaks in socks_connect() --- zyklonb.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/zyklonb.c b/zyklonb.c index e3289d9..55c3246 100644 --- a/zyklonb.c +++ b/zyklonb.c @@ -1018,17 +1018,11 @@ socks_connect (const char *socks_host, const char *socks_port, const char *host, const char *port, const char *username, const char *password, struct error **e) { + int result = -1; struct addrinfo gai_hints, *gai_result; memset (&gai_hints, 0, sizeof gai_hints); gai_hints.ai_socktype = SOCK_STREAM; - int err = getaddrinfo (socks_host, socks_port, &gai_hints, &gai_result); - if (err) - { - error_set (e, "%s: %s", "getaddrinfo", gai_strerror (err)); - return false; - } - unsigned long port_no; const struct servent *serv; if ((serv = getservbyname (port, "tcp"))) @@ -1036,7 +1030,14 @@ socks_connect (const char *socks_host, const char *socks_port, else if (!xstrtoul (&port_no, port, 10) || !port_no || port_no > UINT16_MAX) { error_set (e, "invalid port number"); - return false; + goto fail; + } + + int err = getaddrinfo (socks_host, socks_port, &gai_hints, &gai_result); + if (err) + { + error_set (e, "%s: %s", "getaddrinfo", gai_strerror (err)); + goto fail; } struct socks_data data = @@ -1052,14 +1053,14 @@ socks_connect (const char *socks_host, const char *socks_port, data.address.data.domain = host; } - int fd; - bool success = socks_5_connect (gai_result, &data, &fd, NULL) - || socks_4a_connect (gai_result, &data, &fd, e); + if (!socks_5_connect (gai_result, &data, &result, NULL)) + socks_4a_connect (gai_result, &data, &result, e); - freeaddrinfo (gai_result); if (data.bound_address.type == SOCKS_DOMAIN) free ((char *) data.bound_address.data.domain); - return success ? fd : -1; + freeaddrinfo (gai_result); +fail: + return result; } // --- Plugins ----------------------------------------------------------------- -- cgit v1.2.3-70-g09d2