Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
bugfix: fix buf2signed/buf2unsigned/buf2double bug (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
qinzuoyan authored Jul 17, 2018
1 parent a1060c3 commit d4fb099
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
17 changes: 10 additions & 7 deletions include/dsn/utility/string_conv.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
36 changes: 36 additions & 0 deletions src/core/tests/string_conv_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -76,6 +86,14 @@ TEST(string_conv, buf2int32)
ASSERT_FALSE(dsn::buf2int32(std::to_string(std::numeric_limits<int64_t>::max()), result));
ASSERT_FALSE(dsn::buf2int32(std::to_string(std::numeric_limits<int64_t>::min()), result));
ASSERT_FALSE(dsn::buf2int32(std::to_string(std::numeric_limits<uint64_t>::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)
Expand Down Expand Up @@ -113,6 +131,14 @@ TEST(string_conv, buf2int64)
ASSERT_EQ(result, std::numeric_limits<int64_t>::min());

ASSERT_FALSE(dsn::buf2int64(std::to_string(std::numeric_limits<uint64_t>::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)
Expand All @@ -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);

Expand All @@ -145,6 +173,14 @@ TEST(string_conv, buf2uint64)

ASSERT_TRUE(dsn::buf2uint64(std::to_string(std::numeric_limits<uint64_t>::min()), result));
ASSERT_EQ(result, std::numeric_limits<uint64_t>::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)
Expand Down

0 comments on commit d4fb099

Please sign in to comment.