From d7980d73f76fe3c0f26fe08e342421e3a400cac6 Mon Sep 17 00:00:00 2001 From: qinzuoyan Date: Tue, 17 Jul 2018 21:38:40 +0800 Subject: [PATCH] bugfix: fix buf2signed/buf2unsigned/buf2double bug --- include/dsn/utility/string_conv.h | 17 ++++++++------ src/core/tests/string_conv_test.cpp | 36 +++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/include/dsn/utility/string_conv.h b/include/dsn/utility/string_conv.h index 94771646e0..b89c2ddff7 100644 --- a/include/dsn/utility/string_conv.h +++ b/include/dsn/utility/string_conv.h @@ -44,12 +44,13 @@ bool buf2signed(string_view buf, T &result) return false; } + std::string str(buf.data(), buf.length()); const int saved_errno = errno; errno = 0; char *p = nullptr; - long long v = std::strtoll(buf.data(), &p, 0); + long long v = std::strtoll(str.data(), &p, 0); - if (p - buf.data() != buf.length()) { + if (p - str.data() != str.length()) { return false; } @@ -74,12 +75,13 @@ bool buf2unsigned(string_view buf, T &result) return false; } + std::string str(buf.data(), buf.length()); const int saved_errno = errno; errno = 0; char *p = nullptr; - unsigned long long v = std::strtoull(buf.data(), &p, 0); + unsigned long long v = std::strtoull(str.data(), &p, 0); - if (p - buf.data() != buf.length()) { + if (p - str.data() != str.length()) { return false; } @@ -93,7 +95,7 @@ bool buf2unsigned(string_view buf, T &result) // strtoull() will convert a negative integer to an unsigned integer, // return false in this condition. (but we consider "-0" is correct) - if (v != 0 && std::find(buf.begin(), buf.end(), '-') != buf.end()) { + if (v != 0 && str.find('-') != std::string::npos) { return false; } @@ -142,12 +144,13 @@ inline bool buf2double(string_view buf, double &result) return false; } + std::string str(buf.data(), buf.length()); const int saved_errno = errno; errno = 0; char *p = nullptr; - double v = std::strtod(buf.data(), &p); + double v = std::strtod(str.data(), &p); - if (p - buf.data() != buf.length()) { + if (p - str.data() != str.length()) { return false; } diff --git a/src/core/tests/string_conv_test.cpp b/src/core/tests/string_conv_test.cpp index 3df5c707af..9196d7e91c 100644 --- a/src/core/tests/string_conv_test.cpp +++ b/src/core/tests/string_conv_test.cpp @@ -46,6 +46,16 @@ TEST(string_conv, buf2bool) ASSERT_EQ(result, false); ASSERT_FALSE(dsn::buf2bool("TrUe", result, false)); + + std::string str("true\0false", 10); + ASSERT_FALSE(dsn::buf2bool(dsn::string_view(str.data(), 3), result)); + ASSERT_TRUE(dsn::buf2bool(dsn::string_view(str.data(), 4), result)); + ASSERT_EQ(result, true); + ASSERT_FALSE(dsn::buf2bool(dsn::string_view(str.data(), 5), result)); + ASSERT_FALSE(dsn::buf2bool(dsn::string_view(str.data(), 6), result)); + ASSERT_FALSE(dsn::buf2bool(dsn::string_view(str.data() + 5, 4), result)); + ASSERT_TRUE(dsn::buf2bool(dsn::string_view(str.data() + 5, 5), result)); + ASSERT_EQ(result, false); } TEST(string_conv, buf2int32) @@ -76,6 +86,14 @@ TEST(string_conv, buf2int32) ASSERT_FALSE(dsn::buf2int32(std::to_string(std::numeric_limits::max()), result)); ASSERT_FALSE(dsn::buf2int32(std::to_string(std::numeric_limits::min()), result)); ASSERT_FALSE(dsn::buf2int32(std::to_string(std::numeric_limits::max()), result)); + + std::string str("123\0456", 7); + ASSERT_TRUE(dsn::buf2int32(dsn::string_view(str.data(), 2), result)); + ASSERT_EQ(result, 12); + ASSERT_TRUE(dsn::buf2int32(dsn::string_view(str.data(), 3), result)); + ASSERT_EQ(result, 123); + ASSERT_FALSE(dsn::buf2int32(dsn::string_view(str.data(), 4), result)); + ASSERT_FALSE(dsn::buf2int32(dsn::string_view(str.data(), 5), result)); } TEST(string_conv, buf2int64) @@ -113,6 +131,14 @@ TEST(string_conv, buf2int64) ASSERT_EQ(result, std::numeric_limits::min()); ASSERT_FALSE(dsn::buf2int64(std::to_string(std::numeric_limits::max()), result)); + + std::string str("123\0456", 7); + ASSERT_TRUE(dsn::buf2int64(dsn::string_view(str.data(), 2), result)); + ASSERT_EQ(result, 12); + ASSERT_TRUE(dsn::buf2int64(dsn::string_view(str.data(), 3), result)); + ASSERT_EQ(result, 123); + ASSERT_FALSE(dsn::buf2int64(dsn::string_view(str.data(), 4), result)); + ASSERT_FALSE(dsn::buf2int64(dsn::string_view(str.data(), 5), result)); } TEST(string_conv, buf2uint64) @@ -125,6 +151,8 @@ TEST(string_conv, buf2uint64) ASSERT_TRUE(dsn::buf2uint64("-0", result)); ASSERT_EQ(result, 0); + ASSERT_FALSE(dsn::buf2uint64("-1", result)); + ASSERT_TRUE(dsn::buf2uint64("0xdeadbeef", result)); ASSERT_EQ(result, 0xdeadbeef); @@ -145,6 +173,14 @@ TEST(string_conv, buf2uint64) ASSERT_TRUE(dsn::buf2uint64(std::to_string(std::numeric_limits::min()), result)); ASSERT_EQ(result, std::numeric_limits::min()); + + std::string str("123\0456", 7); + ASSERT_TRUE(dsn::buf2uint64(dsn::string_view(str.data(), 2), result)); + ASSERT_EQ(result, 12); + ASSERT_TRUE(dsn::buf2uint64(dsn::string_view(str.data(), 3), result)); + ASSERT_EQ(result, 123); + ASSERT_FALSE(dsn::buf2uint64(dsn::string_view(str.data(), 4), result)); + ASSERT_FALSE(dsn::buf2uint64(dsn::string_view(str.data(), 5), result)); } TEST(string_conv, int64_partial)