From 5ac6a2c94d36c885f1cdfd473b3bdcdf4c446082 Mon Sep 17 00:00:00 2001 From: Kyle Evans Date: Fri, 21 Feb 2020 18:21:57 +0000 Subject: [PATCH] fetch(3): plug some leaks In the successful case, sockshost is not freed prior to return. The failure case can now be hit after fetch_reopen(), which was not true before. Thus, we need to make sure to clean up all of the conn resources which will also close sd. For all of the points prior to fetch_reopen(), we continue to just close sd. CID: 1419598, 1419616 --- lib/libfetch/common.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c index f6c026049f5c..ae119de32736 100644 --- a/lib/libfetch/common.c +++ b/lib/libfetch/common.c @@ -677,6 +677,7 @@ fetch_connect(const char *host, int port, int af, int verbose) if (sockshost) if (!fetch_socks5_init(conn, host, port, verbose)) goto fail; + free(sockshost); if (cais != NULL) freeaddrinfo(cais); if (sais != NULL) @@ -686,7 +687,10 @@ fetch_connect(const char *host, int port, int af, int verbose) fetch_syserr(); fail: free(sockshost); - if (sd >= 0) + /* Fully close if it was opened; otherwise just don't leak the fd. */ + if (conn != NULL) + fetch_close(conn); + else if (sd >= 0) close(sd); if (cais != NULL) freeaddrinfo(cais);