Move a check for trailing colon to tor_inet_pton()

That way, string_is_valid_ipv6_address() can benefit from it
This commit is contained in:
rl1987 2018-09-01 20:56:33 +03:00 committed by Nick Mathewson
parent 1baa2703c3
commit 742cd15649
3 changed files with 21 additions and 5 deletions

View File

@ -1198,10 +1198,7 @@ tor_addr_parse(tor_addr_t *addr, const char *src)
len -= 2;
}
/* Reject if src has needless trailing ':'. */
if (len > 2 && src[len - 1] == ':' && src[len - 2] != ':') {
result = -1;
} else if (tor_inet_pton(AF_INET6, src, &in6_tmp) > 0) {
if (tor_inet_pton(AF_INET6, src, &in6_tmp) > 0) {
result = AF_INET6;
tor_addr_from_in6(addr, &in6_tmp);
} else if (!brackets_detected &&

View File

@ -168,6 +168,13 @@ tor_inet_pton(int af, const char *src, void *dst)
if (af == AF_INET) {
return tor_inet_aton(src, dst);
} else if (af == AF_INET6) {
ssize_t len = strlen(src);
/* Reject if src has needless trailing ':'. */
if (len > 2 && src[len - 1] == ':' && src[len - 2] != ':') {
return 0;
}
struct in6_addr *out = dst;
uint16_t words[8];
int gapPos = -1, i, setWords=0;
@ -207,7 +214,6 @@ tor_inet_pton(int af, const char *src, void *dst)
return 0;
if (TOR_ISXDIGIT(*src)) {
char *next;
ssize_t len;
long r = strtol(src, &next, 16);
if (next == NULL || next == src) {
/* The 'next == src' error case can happen on versions of openbsd

View File

@ -5749,6 +5749,18 @@ test_util_ipv4_validation(void *arg)
return;
}
static void
test_util_ipv6_validation(void *arg)
{
(void)arg;
tt_assert(string_is_valid_ipv6_address("2a00:1450:401b:800::200e"));
tt_assert(!string_is_valid_ipv6_address("11:22::33:44:"));
done:
return;
}
static void
test_util_writepid(void *arg)
{
@ -6439,6 +6451,7 @@ struct testcase_t util_tests[] = {
UTIL_TEST(hostname_validation, 0),
UTIL_TEST(dest_validation_edgecase, 0),
UTIL_TEST(ipv4_validation, 0),
UTIL_TEST(ipv6_validation, 0),
UTIL_TEST(writepid, 0),
UTIL_TEST(get_avail_disk_space, 0),
UTIL_TEST(touch_file, 0),