This command should result in an error, but doesn't. Notice there are four digits in the last octet of the bogus address [138.201.14.1979. saxatile's IP address is 138.201.14.197. tor_addr_parse is throwing away the final character, and therefore failing to notice that the address is bad, because the string starts with [.
Great, rl1987! If you don't mind review from the peanut gallery:
- if (src[0] == '[' && src[1])+ if (src[0] == '[' && src[1] && src[strlen(src)-1] == ']') {
I think the condition src[1] is now redundant. It was meant to ensure that the string contains at least 2 characters, but the newly added condition also does that.
I note that the condition brackets_detected != 0 is equivalent to tmp != NULL. But actually, I don't think I would try to remove the brackets_detected variable, because it expresses clearly what it means.
I think the tor_inet_pton(AF_INET6, ...) line could use a comment saying that IPv6 addresses are allowed to be with or without brackets.
+ /* 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) {
I wonder, does this check for a trailing colon belong in tor_inet_pton instead? Or is there a reason for tor_inet_pton to accept such inputs? Maybe for compatibility with inet_pton? It's surprising to me that this check is necessary; I would have assumed (wrongly) that the underlying address parser would reject it.
Great, rl1987! If you don't mind review from the peanut gallery:
{{{#!diff
if (src[0] == '[' && src[1])
if (src[0] == '[' && src[1] && src[strlen(src)-1] == ']') {
}}}
I think the condition src[1] is now redundant. It was meant to ensure that the string contains at least 2 characters, but the newly added condition also does that.
I made some cleanups in ad165f7aef4e2feefd6d4cde5d57dfd4fa6d55f5.
I note that the condition brackets_detected != 0 is equivalent to tmp != NULL. But actually, I don't think I would try to remove the brackets_detected variable, because it expresses clearly what it means.
That's correct. However, I'm keeping tmp variable here, as we may need to free the temporary bracketless string that tor_strndup allocated if brackets were detected. Let's have both variables.
I think the tor_inet_pton(AF_INET6, ...) line could use a comment saying that IPv6 addresses are allowed to be with or without brackets.
Well, function documentation for tor_addr_parse() mentions that already. I don't think that additional comment is needed.
} else if (tor_inet_pton(AF_INET6, src, &in6_tmp) > 0) {
}}}
I wonder, does this check for a trailing colon belong in tor_inet_pton instead? Or is there a reason for tor_inet_pton to accept such inputs? Maybe for compatibility with inet_pton? It's surprising to me that this check is necessary; I would have assumed (wrongly) that the underlying address parser would reject it.
That's a good idea, as other code that depends on tor_inet_pton will be able to benefit from that check (such as string_is_valid_ipv6_address() function). So I moved the check in 5438ff3e13a803bf24ba98e11a0b1a6b3d839cc9. I'm not sure if that breaks any standard compatibility though.