[tor-commits] [torsocks/master] Fix: check strdup return value

dgoulet at torproject.org dgoulet at torproject.org
Fri Apr 4 22:40:27 UTC 2014


commit c54460b94e2a552a9e1fbe65c9f34210d4d1277a
Author: David Goulet <dgoulet at ev0ke.net>
Date:   Thu Feb 20 11:05:57 2014 +0000

    Fix: check strdup return value
    
    Reported-by: Nick Mathewson <nickm at torproject.org>
    Signed-off-by: David Goulet <dgoulet at ev0ke.net>
---
 src/common/utils.c |    8 ++++++++
 src/lib/connect.c  |    6 +++++-
 src/lib/torsocks.c |    4 ++++
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/common/utils.c b/src/common/utils.c
index 578d290..3a62f6c 100644
--- a/src/common/utils.c
+++ b/src/common/utils.c
@@ -125,6 +125,10 @@ int utils_tokenize_ignore_comments(const char *_line, size_t size, char **tokens
 	assert(tokens);
 
 	line = strdup(_line);
+	if (!line) {
+		ret = -ENOMEM;
+		goto error;
+	}
 
 	/* Ignore line if it starts with a # meaning a comment. */
 	if (*line == '#') {
@@ -149,6 +153,10 @@ int utils_tokenize_ignore_comments(const char *_line, size_t size, char **tokens
 	c = strtok(line, " \t");
 	while (c != NULL) {
 		tokens[i] = strdup(c);
+		if (!tokens[i]) {
+			ret = -ENOMEM;
+			goto error;
+		}
 		c = strtok(NULL, " \t");
 		i++;
 	}
diff --git a/src/lib/connect.c b/src/lib/connect.c
index e47ab38..03f6900 100644
--- a/src/lib/connect.c
+++ b/src/lib/connect.c
@@ -113,8 +113,12 @@ LIBC_CONNECT_RET_TYPE tsocks_connect(LIBC_CONNECT_SIG)
 			goto error;
 		}
 		new_conn->dest_addr.domain = CONNECTION_DOMAIN_NAME;
-		new_conn->dest_addr.hostname.addr = strdup(on_entry->hostname);
 		new_conn->dest_addr.hostname.port = inet_addr->sin_port;
+		new_conn->dest_addr.hostname.addr = strdup(on_entry->hostname);
+		if (!new_conn->dest_addr.hostname.addr) {
+			errno = ENOMEM;
+			goto error;
+		}
 	} else {
 		/*
 		 * Check if address is localhost. At this point, we are sure it's not a
diff --git a/src/lib/torsocks.c b/src/lib/torsocks.c
index ba7142d..654f825 100644
--- a/src/lib/torsocks.c
+++ b/src/lib/torsocks.c
@@ -92,6 +92,10 @@ static void init_config(void)
 	 */
 	if (!tsocks_config.conf_file.tor_address) {
 		tsocks_config.conf_file.tor_address = strdup(DEFAULT_TOR_ADDRESS);
+		if (!tsocks_config.conf_file.tor_address) {
+			/* Most likely ENOMEM thus we can't continue. */
+			clean_exit(EXIT_FAILURE);
+		}
 	}
 	if (tsocks_config.conf_file.tor_port == 0) {
 		tsocks_config.conf_file.tor_port = DEFAULT_TOR_PORT;





More information about the tor-commits mailing list