summaryrefslogtreecommitdiff
path: root/lib/libfetch
diff options
context:
space:
mode:
authorKyle Evans <kevans@FreeBSD.org>2020-02-15 19:47:49 +0000
committerKyle Evans <kevans@FreeBSD.org>2020-02-15 19:47:49 +0000
commit86fd2105dcebdecbcaf36d634d79515ace012e8f (patch)
tree37f5543da5b71369f4027b1157f054325c40346e /lib/libfetch
parent3dc455e8974c1a4723f6703f47b2f667a61c934b (diff)
downloadsrc-test-86fd2105dcebdecbcaf36d634d79515ace012e8f.tar.gz
src-test-86fd2105dcebdecbcaf36d634d79515ace012e8f.zip
fetch(3): don't leak sockshost on failure
fetch_socks5_getenv will allocate memory for the host (or set it to NULL) in all cases through the function; the caller is responsible for freeing it if we end up allocating. While I'm here, I've eliminated a label that just jumps to the next line...
Notes
Notes: svn path=/head/; revision=357979
Diffstat (limited to 'lib/libfetch')
-rw-r--r--lib/libfetch/common.c23
1 files changed, 13 insertions, 10 deletions
diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c
index 7dbb032a80674..f6c026049f5cf 100644
--- a/lib/libfetch/common.c
+++ b/lib/libfetch/common.c
@@ -557,8 +557,10 @@ fetch_socks5_getenv(char **host, int *port)
*host = strndup(socks5env, ext - socks5env);
}
- if (*host == NULL)
- goto fail;
+ if (*host == NULL) {
+ fprintf(stderr, "Failure to allocate memory, exiting.\n");
+ return (-1);
+ }
if (ext == NULL) {
*port = 1080; /* Default port as defined in RFC1928 */
} else {
@@ -567,16 +569,14 @@ fetch_socks5_getenv(char **host, int *port)
*port = strtoimax(ext, (char **)&endptr, 10);
if (*endptr != '\0' || errno != 0 || *port < 0 ||
*port > 65535) {
+ free(*host);
+ *host = NULL;
socks5_seterr(SOCKS5_ERR_BAD_PORT);
return (0);
}
}
return (2);
-
-fail:
- fprintf(stderr, "Failure to allocate memory, exiting.\n");
- return (-1);
}
@@ -595,7 +595,11 @@ fetch_connect(const char *host, int port, int af, int verbose)
DEBUGF("---> %s:%d\n", host, port);
- /* Check if SOCKS5_PROXY env variable is set */
+ /*
+ * Check if SOCKS5_PROXY env variable is set. fetch_socks5_getenv
+ * will either set sockshost = NULL or allocate memory in all cases.
+ */
+ sockshost = NULL;
if (!fetch_socks5_getenv(&sockshost, &socksport))
goto fail;
@@ -662,7 +666,7 @@ fetch_connect(const char *host, int port, int af, int verbose)
"failed to connect to SOCKS5 server %s:%d",
sockshost, socksport);
socks5_seterr(SOCKS5_ERR_CONN_REFUSED);
- goto syserr1;
+ goto fail;
}
goto syserr;
}
@@ -680,9 +684,8 @@ fetch_connect(const char *host, int port, int af, int verbose)
return (conn);
syserr:
fetch_syserr();
-syserr1:
- goto fail;
fail:
+ free(sockshost);
if (sd >= 0)
close(sd);
if (cais != NULL)