diff --git a/src/butil/endpoint.cpp b/src/butil/endpoint.cpp index d243252d6d..a8d8936c63 100644 --- a/src/butil/endpoint.cpp +++ b/src/butil/endpoint.cpp @@ -288,7 +288,7 @@ int str2endpoint(const char* str, EndPoint* point) { if (end == str + i) { return -1; } else if (*end) { - for (++end; isspace(*end); ++end); + for (; isspace(*end); ++end); if (*end) { return -1; } diff --git a/test/endpoint_unittest.cpp b/test/endpoint_unittest.cpp index cf47131599..af3098f475 100644 --- a/test/endpoint_unittest.cpp +++ b/test/endpoint_unittest.cpp @@ -115,6 +115,19 @@ TEST(EndPointTest, endpoint) { ASSERT_EQ(289, p6.port); #endif } +TEST(EndPointTest, endpoint_reject_trailing_characters_after_port) { + butil::EndPoint ep; + + // invalid: non-whitespace after port + ASSERT_EQ(-1, butil::str2endpoint("127.0.0.1:8000a", &ep)); + ASSERT_EQ(-1, butil::str2endpoint("127.0.0.1:8000#", &ep)); + ASSERT_EQ(-1, butil::str2endpoint("127.0.0.1:8000abc", &ep)); + + // valid: only whitespace after port + ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:8000 ", &ep)); + ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:8000\t", &ep)); + ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:8000\n", &ep)); +} TEST(EndPointTest, hash_table) { butil::hash_map m;