svn commit: r357977 - head/lib/libfetch

Kyle Evans kevans at FreeBSD.org
Sat Feb 15 19:31:41 UTC 2020


Author: kevans
Date: Sat Feb 15 19:31:40 2020
New Revision: 357977
URL: https://svnweb.freebsd.org/changeset/base/357977

Log:
  fetch(3): move bits of fetch_socks5_getenv around
  
  This commit separates out port parsing and validation from grabbing the host
  from the env var. The only related bit really is that we need to be more
  specific with the delimiter in the IPv6 case.

Modified:
  head/lib/libfetch/common.c

Modified: head/lib/libfetch/common.c
==============================================================================
--- head/lib/libfetch/common.c	Sat Feb 15 19:15:24 2020	(r357976)
+++ head/lib/libfetch/common.c	Sat Feb 15 19:31:40 2020	(r357977)
@@ -525,54 +525,50 @@ int
 fetch_socks5_getenv(char **host, int *port)
 {
 	char *socks5env, *endptr, *ext;
+	const char *portDelim;
+	size_t slen;
 
+	portDelim = ":";
 	if ((socks5env = getenv("SOCKS5_PROXY")) == NULL || *socks5env == '\0') {
 		*host = NULL;
 		*port = -1;
 		return (-1);
 	}
 
-	/* IPv6 addresses begin and end in brackets */
+	/*
+	 * IPv6 addresses begin and end in brackets.  Set the port delimiter
+	 * accordingly and search for it so we can do appropriate validation.
+	 */
+	if (socks5env[0] == '[')
+		portDelim = "]:";
+
+	slen = strlen(socks5env);
+	ext = strstr(socks5env, portDelim);
 	if (socks5env[0] == '[') {
-		if (socks5env[strlen(socks5env) - 1] == ']') {
-			*host = strndup(socks5env, strlen(socks5env));
+		if (socks5env[slen - 1] == ']') {
+			*host = strndup(socks5env, slen);
 			if (*host == NULL)
 				goto fail;
-			*port = 1080; /* Default port as defined in RFC1928 */
-		} else {
-			ext = strstr(socks5env, "]:");
-			if (ext == NULL) {
-				socks5_seterr(SOCKS5_ERR_BAD_PROXY_FORMAT);
-				return (0);
-			}
-			ext=ext+1;
-			*host = strndup(socks5env, ext - socks5env);
-			if (*host == NULL)
-				goto fail;
-			errno = 0;
-			*port = strtoimax(ext + 1, (char **)&endptr, 10);
-			if (*endptr != '\0' || errno != 0 || *port < 0 ||
-			    *port > 65535) {
-				socks5_seterr(SOCKS5_ERR_BAD_PORT);
-				return (0);
-			}
+		} else if (ext == NULL) {
+			socks5_seterr(SOCKS5_ERR_BAD_PROXY_FORMAT);
+			return (0);
 		}
 	} else {
-		ext = strrchr(socks5env, ':');
-		if (ext == NULL) {
-			*host = strdup(socks5env);
-			*port = 1080;
-		} else {
-			*host = strndup(socks5env, ext-socks5env);
-			if (*host == NULL)
-				goto fail;
-			errno = 0;
-			*port = strtoimax(ext + 1, (char **)&endptr, 10);
-			if (*endptr != '\0' || errno != 0 || *port < 0 ||
-			    *port > 65535) {
-				socks5_seterr(SOCKS5_ERR_BAD_PORT);
-				return (0);
-			}
+		*host = strndup(socks5env, ext - socks5env);
+		if (*host == NULL)
+			goto fail;
+	}
+
+	if (ext == NULL) {
+		*port = 1080; /* Default port as defined in RFC1928 */
+	} else {
+		ext += strlen(portDelim);
+		errno = 0;
+		*port = strtoimax(ext, (char **)&endptr, 10);
+		if (*endptr != '\0' || errno != 0 || *port < 0 ||
+		    *port > 65535) {
+			socks5_seterr(SOCKS5_ERR_BAD_PORT);
+			return (0);
 		}
 	}
 


More information about the svn-src-head mailing list