diff --git a/ChangeLog b/ChangeLog index ffe6a8b57..411eedc59 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,6 @@ 1.1.1 release + * fixed parsing of IPv6 endpoint with invalid port character separator * fixed dht stats counters that weren't being updated * make sure add_torrent_alert is always posted before other alerts for the torrent diff --git a/src/socket_io.cpp b/src/socket_io.cpp index ba387b38e..bbea47397 100644 --- a/src/socket_io.cpp +++ b/src/socket_io.cpp @@ -105,6 +105,11 @@ namespace libtorrent } *port_pos = '\0'; ++port_pos; + if (port_pos == str.end() || *port_pos != ':') + { + ec = errors::invalid_port; + return ret; + } #if TORRENT_USE_IPV6 ret.address(address_v6::from_string(&*start, ec)); #else @@ -125,12 +130,13 @@ namespace libtorrent if (ec) return ret; } + ++port_pos; if (port_pos == str.end()) { ec = errors::invalid_port; return ret; } - ++port_pos; + ret.port(std::atoi(&*port_pos)); return ret; } diff --git a/test/test_socket_io.cpp b/test/test_socket_io.cpp index 98793695e..e333a74aa 100644 --- a/test/test_socket_io.cpp +++ b/test/test_socket_io.cpp @@ -114,3 +114,58 @@ TORRENT_TEST(socket_io) TEST_EQUAL(list[0], udp::endpoint(address_v4::from_string("16.5.128.1"), 1337)); } +TORRENT_TEST(parse_invalid_ipv4_endpoint) +{ + error_code ec; + tcp::endpoint endp; + + endp = parse_endpoint("127.0.0.1-4", ec); + TEST_CHECK(ec); + ec.clear(); + + endp = parse_endpoint("127.0.0.1", ec); + TEST_CHECK(ec); + ec.clear(); + + endp = parse_endpoint("127.0.0.1:", ec); + TEST_CHECK(ec); + ec.clear(); + + endp = parse_endpoint("127.0.0.1X", ec); + TEST_CHECK(ec); + ec.clear(); + + endp = parse_endpoint("127.0.0.1:4", ec); + TEST_CHECK(!ec); + TEST_EQUAL(endp, ep("127.0.0.1", 4)); + ec.clear(); +} + +#if TORRENT_USE_IPV6 +TORRENT_TEST(parse_invalid_ipv6_endpoint) +{ + error_code ec; + tcp::endpoint endp; + + endp = parse_endpoint("[::1]-4", ec); + TEST_CHECK(ec); + ec.clear(); + + endp = parse_endpoint("[::1]", ec); + TEST_CHECK(ec); + ec.clear(); + + endp = parse_endpoint("[::1]:", ec); + TEST_CHECK(ec); + ec.clear(); + + endp = parse_endpoint("[::1]X", ec); + TEST_CHECK(ec); + ec.clear(); + + endp = parse_endpoint("[::1]:4", ec); + TEST_CHECK(!ec); + TEST_EQUAL(endp, ep("::1", 4)); + ec.clear(); +} +#endif