From f334f5b6a633df52d38bad18aea95be99292a38c Mon Sep 17 00:00:00 2001 From: Ilya Zverev Date: Wed, 1 Jun 2016 14:45:20 +0300 Subject: [PATCH] [tests] Correctly process empty strings in to_whatever --- base/base_tests/string_utils_test.cpp | 3 +++ base/string_utils.cpp | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/base/base_tests/string_utils_test.cpp b/base/base_tests/string_utils_test.cpp index 684a33a527..aa7e79074d 100644 --- a/base/base_tests/string_utils_test.cpp +++ b/base/base_tests/string_utils_test.cpp @@ -238,6 +238,9 @@ UNIT_TEST(to_uint64) uint64_t i; string s; + s = ""; + TEST(!strings::to_uint64(s, i), ()); + s = "0"; TEST(strings::to_uint64(s, i), ()); TEST_EQUAL(0, i, ()); diff --git a/base/string_utils.cpp b/base/string_utils.cpp index cd9a1101ad..d5fe4d7c43 100644 --- a/base/string_utils.cpp +++ b/base/string_utils.cpp @@ -46,7 +46,7 @@ namespace template bool IntegerCheck(char const * start, char const * stop, T x, TResult & out) { - if (errno != EINVAL && *stop == 0) + if (errno != EINVAL && *stop == 0 && start != stop) { out = static_cast(x); return static_cast(out) == x; @@ -80,7 +80,7 @@ bool to_uint64(char const * s, uint64_t & i) #else i = strtoull(s, &stop, 10); #endif - return *stop == 0; + return *stop == 0 && s != stop; } bool to_int64(char const * s, int64_t & i) @@ -91,7 +91,7 @@ bool to_int64(char const * s, int64_t & i) #else i = strtoll(s, &stop, 10); #endif - return *stop == 0; + return *stop == 0 && s != stop; } bool to_double(char const * s, double & d)