From d2c331cd47d106651dae9aa3cac4dd0012b279f5 Mon Sep 17 00:00:00 2001 From: Anant Shukla Date: Fri, 9 Jan 2026 16:17:05 +0530 Subject: [PATCH 1/2] Fix port parsing validation in str2endpoint Signed-off-by: Anant Shukla --- src/butil/endpoint.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; } From 0cad67acd1216a3852f7321b4d88a511c64caf04 Mon Sep 17 00:00:00 2001 From: Anant Shukla Date: Sat, 10 Jan 2026 13:57:00 +0530 Subject: [PATCH 2/2] Add unit tests for rejecting trailing characters after port parsing Signed-off-by: Anant Shukla --- test/endpoint_unittest.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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;