Skip to content

Commit 9719654

Browse files
Fix port parsing validation in str2endpoint (#3193)
* Fix port parsing validation in str2endpoint Signed-off-by: Anant Shukla <anantshukla836@gmail.com> * Add unit tests for rejecting trailing characters after port parsing Signed-off-by: Anant Shukla <anantshukla836@gmail.com> --------- Signed-off-by: Anant Shukla <anantshukla836@gmail.com>
1 parent c7973d0 commit 9719654

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/butil/endpoint.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ int str2endpoint(const char* str, EndPoint* point) {
288288
if (end == str + i) {
289289
return -1;
290290
} else if (*end) {
291-
for (++end; isspace(*end); ++end);
291+
for (; isspace(*end); ++end);
292292
if (*end) {
293293
return -1;
294294
}

test/endpoint_unittest.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,19 @@ TEST(EndPointTest, endpoint) {
115115
ASSERT_EQ(289, p6.port);
116116
#endif
117117
}
118+
TEST(EndPointTest, endpoint_reject_trailing_characters_after_port) {
119+
butil::EndPoint ep;
120+
121+
// invalid: non-whitespace after port
122+
ASSERT_EQ(-1, butil::str2endpoint("127.0.0.1:8000a", &ep));
123+
ASSERT_EQ(-1, butil::str2endpoint("127.0.0.1:8000#", &ep));
124+
ASSERT_EQ(-1, butil::str2endpoint("127.0.0.1:8000abc", &ep));
125+
126+
// valid: only whitespace after port
127+
ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:8000 ", &ep));
128+
ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:8000\t", &ep));
129+
ASSERT_EQ(0, butil::str2endpoint("127.0.0.1:8000\n", &ep));
130+
}
118131

119132
TEST(EndPointTest, hash_table) {
120133
butil::hash_map<butil::EndPoint, int> m;

0 commit comments

Comments
 (0)