From b0ba4aa7e98af030e0e1be19a58ab7a6f00fa423 Mon Sep 17 00:00:00 2001 From: rl1987 Date: Mon, 12 Feb 2018 19:52:47 +0100 Subject: [PATCH] Fix bracketed IPv6 string validation --- src/common/util.c | 15 ++++++++++++++- src/test/test_socks.c | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/common/util.c b/src/common/util.c index 7c715fb3cd..ea0ec3daee 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -1079,9 +1079,22 @@ string_is_valid_ipv6_address(const char *string) int string_is_valid_dest(const char *string) { - return string_is_valid_ipv4_address(string) || + char *tmp = NULL; + int retval; + + tor_assert(string); + tor_assert(strlen(string) > 0); + + if (string[0] == '[' && string[strlen(string) - 1] == ']') + string = tmp = tor_strndup(string + 1, strlen(string) - 2); + + retval = string_is_valid_ipv4_address(string) || string_is_valid_ipv6_address(string) || string_is_valid_hostname(string); + + tor_free(tmp); + + return retval; } /** Return true iff string matches a pattern of DNS names diff --git a/src/test/test_socks.c b/src/test/test_socks.c index 70509e43e7..3f9cc887b6 100644 --- a/src/test/test_socks.c +++ b/src/test/test_socks.c @@ -355,7 +355,7 @@ test_socks_5_supported_commands(void *ptr) ADD_DATA(buf, "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]"); ADD_DATA(buf, "\x01\x02"); tt_int_op(fetch_from_buf_socks(buf, socks, get_options()->TestSocks, 1), - OP_EQ, -1); + OP_EQ, 1); tt_str_op("[2001:0db8:85a3:0000:0000:8a2e:0370:7334]", OP_EQ, socks->address);