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...
This commit is contained in:
Kyle Evans 2020-02-15 19:47:49 +00:00
parent 3dc455e897
commit 86fd2105dc

View File

@ -557,8 +557,10 @@ fetch_socks5_getenv(char **host, int *port)
*host = strndup(socks5env, ext - socks5env); *host = strndup(socks5env, ext - socks5env);
} }
if (*host == NULL) if (*host == NULL) {
goto fail; fprintf(stderr, "Failure to allocate memory, exiting.\n");
return (-1);
}
if (ext == NULL) { if (ext == NULL) {
*port = 1080; /* Default port as defined in RFC1928 */ *port = 1080; /* Default port as defined in RFC1928 */
} else { } else {
@ -567,16 +569,14 @@ fetch_socks5_getenv(char **host, int *port)
*port = strtoimax(ext, (char **)&endptr, 10); *port = strtoimax(ext, (char **)&endptr, 10);
if (*endptr != '\0' || errno != 0 || *port < 0 || if (*endptr != '\0' || errno != 0 || *port < 0 ||
*port > 65535) { *port > 65535) {
free(*host);
*host = NULL;
socks5_seterr(SOCKS5_ERR_BAD_PORT); socks5_seterr(SOCKS5_ERR_BAD_PORT);
return (0); return (0);
} }
} }
return (2); 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); 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)) if (!fetch_socks5_getenv(&sockshost, &socksport))
goto fail; 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", "failed to connect to SOCKS5 server %s:%d",
sockshost, socksport); sockshost, socksport);
socks5_seterr(SOCKS5_ERR_CONN_REFUSED); socks5_seterr(SOCKS5_ERR_CONN_REFUSED);
goto syserr1; goto fail;
} }
goto syserr; goto syserr;
} }
@ -680,9 +684,8 @@ fetch_connect(const char *host, int port, int af, int verbose)
return (conn); return (conn);
syserr: syserr:
fetch_syserr(); fetch_syserr();
syserr1:
goto fail;
fail: fail:
free(sockshost);
if (sd >= 0) if (sd >= 0)
close(sd); close(sd);
if (cais != NULL) if (cais != NULL)