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

chenBright 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 1f7cfeb7c Respect splitter field bounds in numeric conversions (#3660)
1f7cfeb7c is described below

commit 1f7cfeb7c706763fddc5df5825ccb4229f0b8180
Author: Xiaofeng Wang <[email protected]>
AuthorDate: Mon Nov 9 23:48:22 2026 +0800

    Respect splitter field bounds in numeric conversions (#3660)
---
 src/butil/string_splitter.h       |  4 +--
 src/butil/string_splitter_inl.h   | 60 +++++++++++++++++++++++----------------
 test/string_splitter_unittest.cpp | 40 ++++++++++++++++++++++++++
 3 files changed, 78 insertions(+), 26 deletions(-)

diff --git a/src/butil/string_splitter.h b/src/butil/string_splitter.h
index 0651e68dc..0fc8bb272 100644
--- a/src/butil/string_splitter.h
+++ b/src/butil/string_splitter.h
@@ -88,7 +88,7 @@ public:
 
     // Cast field to specific type, and write the value into `pv'.
     // Returns 0 on success, -1 otherwise.
-    // NOTE: If separator is a digit, casting functions always return -1.
+    // Only the bytes in the current field are converted.
     inline int to_int8(int8_t *pv) const;
     inline int to_uint8(uint8_t *pv) const;
     inline int to_int(int *pv) const;
@@ -143,7 +143,7 @@ public:
 
     // Cast field to specific type, and write the value into `pv'.
     // Returns 0 on success, -1 otherwise.
-    // NOTE: If separators contains digit, casting functions always return -1.
+    // Only the bytes in the current field are converted.
     inline int to_int8(int8_t *pv) const;
     inline int to_uint8(uint8_t *pv) const;
     inline int to_int(int *pv) const;
diff --git a/src/butil/string_splitter_inl.h b/src/butil/string_splitter_inl.h
index ed2981fcb..ef51b9a78 100644
--- a/src/butil/string_splitter_inl.h
+++ b/src/butil/string_splitter_inl.h
@@ -132,39 +132,45 @@ int StringSplitter::to_uint(unsigned int* pv) const {
 }
 
 int StringSplitter::to_long(long* pv) const {
+    const std::string value(field(), length());
     char* endptr = nullptr;
-    *pv = strtol(field(), &endptr, 10);
-    return (endptr == field() + length()) ? 0 : -1;
+    *pv = strtol(value.c_str(), &endptr, 10);
+    return (endptr == value.c_str() + value.size()) ? 0 : -1;
 }
 
 int StringSplitter::to_ulong(unsigned long* pv) const {
+    const std::string value(field(), length());
     char* endptr = nullptr;
-    *pv = strtoul(field(), &endptr, 10);
-    return (endptr == field() + length()) ? 0 : -1;
+    *pv = strtoul(value.c_str(), &endptr, 10);
+    return (endptr == value.c_str() + value.size()) ? 0 : -1;
 }
 
 int StringSplitter::to_longlong(long long* pv) const {
+    const std::string value(field(), length());
     char* endptr = nullptr;
-    *pv = strtoll(field(), &endptr, 10);
-    return (endptr == field() + length()) ? 0 : -1;
+    *pv = strtoll(value.c_str(), &endptr, 10);
+    return (endptr == value.c_str() + value.size()) ? 0 : -1;
 }
 
 int StringSplitter::to_ulonglong(unsigned long long* pv) const {
+    const std::string value(field(), length());
     char* endptr = nullptr;
-    *pv = strtoull(field(), &endptr, 10);
-    return (endptr == field() + length()) ? 0 : -1;
+    *pv = strtoull(value.c_str(), &endptr, 10);
+    return (endptr == value.c_str() + value.size()) ? 0 : -1;
 }
 
 int StringSplitter::to_float(float* pv) const {
+    const std::string value(field(), length());
     char* endptr = nullptr;
-    *pv = strtof(field(), &endptr);
-    return (endptr == field() + length()) ? 0 : -1;
+    *pv = strtof(value.c_str(), &endptr);
+    return (endptr == value.c_str() + value.size()) ? 0 : -1;
 }
 
 int StringSplitter::to_double(double* pv) const {
+    const std::string value(field(), length());
     char* endptr = nullptr;
-    *pv = strtod(field(), &endptr);
-    return (endptr == field() + length()) ? 0 : -1;
+    *pv = strtod(value.c_str(), &endptr);
+    return (endptr == value.c_str() + value.size()) ? 0 : -1;
 }
 
 StringMultiSplitter::StringMultiSplitter (
@@ -283,39 +289,45 @@ int StringMultiSplitter::to_uint(unsigned int* pv) const {
 }
 
 int StringMultiSplitter::to_long(long* pv) const {
+    const std::string value(field(), length());
     char* endptr = nullptr;
-    *pv = strtol(field(), &endptr, 10);
-    return (endptr == field() + length()) ? 0 : -1;
+    *pv = strtol(value.c_str(), &endptr, 10);
+    return (endptr == value.c_str() + value.size()) ? 0 : -1;
 }
 
 int StringMultiSplitter::to_ulong(unsigned long* pv) const {
+    const std::string value(field(), length());
     char* endptr = nullptr;
-    *pv = strtoul(field(), &endptr, 10);
-    return (endptr == field() + length()) ? 0 : -1;
+    *pv = strtoul(value.c_str(), &endptr, 10);
+    return (endptr == value.c_str() + value.size()) ? 0 : -1;
 }
 
 int StringMultiSplitter::to_longlong(long long* pv) const {
+    const std::string value(field(), length());
     char* endptr = nullptr;
-    *pv = strtoll(field(), &endptr, 10);
-    return (endptr == field() + length()) ? 0 : -1;
+    *pv = strtoll(value.c_str(), &endptr, 10);
+    return (endptr == value.c_str() + value.size()) ? 0 : -1;
 }
 
 int StringMultiSplitter::to_ulonglong(unsigned long long* pv) const {
+    const std::string value(field(), length());
     char* endptr = nullptr;
-    *pv = strtoull(field(), &endptr, 10);
-    return (endptr == field() + length()) ? 0 : -1;
+    *pv = strtoull(value.c_str(), &endptr, 10);
+    return (endptr == value.c_str() + value.size()) ? 0 : -1;
 }
 
 int StringMultiSplitter::to_float(float* pv) const {
+    const std::string value(field(), length());
     char* endptr = nullptr;
-    *pv = strtof(field(), &endptr);
-    return (endptr == field() + length()) ? 0 : -1;
+    *pv = strtof(value.c_str(), &endptr);
+    return (endptr == value.c_str() + value.size()) ? 0 : -1;
 }
 
 int StringMultiSplitter::to_double(double* pv) const {
+    const std::string value(field(), length());
     char* endptr = nullptr;
-    *pv = strtod(field(), &endptr);
-    return (endptr == field() + length()) ? 0 : -1;
+    *pv = strtod(value.c_str(), &endptr);
+    return (endptr == value.c_str() + value.size()) ? 0 : -1;
 }
 
 void KeyValuePairsSplitter::UpdateDelimiterPosition() {
diff --git a/test/string_splitter_unittest.cpp 
b/test/string_splitter_unittest.cpp
index bac31c43e..dc1fd64e2 100644
--- a/test/string_splitter_unittest.cpp
+++ b/test/string_splitter_unittest.cpp
@@ -246,6 +246,46 @@ TEST_F(StringSplitterTest, number_list) {
     ASSERT_EQ(ss2.field(), str + str_len);
 }
 
+template <typename Splitter>
+void CheckBoundedNumericField(const Splitter& splitter) {
+    long l;
+    unsigned long ul;
+    long long ll;
+    unsigned long long ull;
+    float f;
+    double d;
+    EXPECT_EQ(0, splitter.to_long(&l));
+    EXPECT_EQ(123, l);
+    EXPECT_EQ(0, splitter.to_ulong(&ul));
+    EXPECT_EQ(123u, ul);
+    EXPECT_EQ(0, splitter.to_longlong(&ll));
+    EXPECT_EQ(123, ll);
+    EXPECT_EQ(0, splitter.to_ulonglong(&ull));
+    EXPECT_EQ(123u, ull);
+    EXPECT_EQ(0, splitter.to_float(&f));
+    EXPECT_FLOAT_EQ(123, f);
+    EXPECT_EQ(0, splitter.to_double(&d));
+    EXPECT_DOUBLE_EQ(123, d);
+}
+
+TEST_F(StringSplitterTest, numeric_conversions_respect_field_end) {
+    // The suffix is numeric too: parsing must stop at the explicit end,
+    // rather than consume it and reject the bounded field afterwards.
+    const char text[] = "123456";
+    CheckBoundedNumericField(butil::StringSplitter(text, text + 3, ','));
+    CheckBoundedNumericField(butil::StringMultiSplitter(text, text + 3, ",;"));
+
+    // These bytes have no trailing NUL. Sanitizers also check the read bound.
+    const std::string digits = "123";
+    char* bounded = new char[digits.size()];
+    memcpy(bounded, digits.data(), digits.size());
+    CheckBoundedNumericField(butil::StringSplitter(
+        bounded, bounded + digits.size(), ','));
+    CheckBoundedNumericField(butil::StringMultiSplitter(
+        bounded, bounded + digits.size(), ",;"));
+    delete[] bounded;
+}
+
 TEST_F(StringSplitterTest, cast_type) {
     const char* str = "-1\t123\t111\t1\t10\t11\t1.3\t3.1415926\t127\t128\t256";
     int i = 0;


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

Reply via email to