This is an automated email from the ASF dual-hosted git repository.

xiaofeng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 97196544 Fix port parsing validation in str2endpoint (#3193)
97196544 is described below

commit 971965444630c9461e5a9cd97556a548d6abc873
Author: Anant Shukla <[email protected]>
AuthorDate: Mon Jan 12 09:50:13 2026 +0530

    Fix port parsing validation in str2endpoint (#3193)
    
    * Fix port parsing validation in str2endpoint
    
    Signed-off-by: Anant Shukla <[email protected]>
    
    * Add unit tests for rejecting trailing characters after port parsing
    
    Signed-off-by: Anant Shukla <[email protected]>
    
    ---------
    
    Signed-off-by: Anant Shukla <[email protected]>
---
 src/butil/endpoint.cpp     |  2 +-
 test/endpoint_unittest.cpp | 13 +++++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/butil/endpoint.cpp b/src/butil/endpoint.cpp
index d243252d..a8d8936c 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 cf471315..af3098f4 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<butil::EndPoint, int> m;


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to