aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarcoFalke <falke.marco@gmail.com>2021-10-01 17:33:35 +0200
committerMarcoFalke <falke.marco@gmail.com>2021-10-01 18:05:33 +0200
commitfa3cd2853530c86c261ac7266ffe4f1726fe9ce6 (patch)
tree6defa00c71da38b5286979f5fa8e4ae110223a2a /src
parent35a31d5f7e9cd71a210c1ed10abc9d772ff36049 (diff)
downloadbitcoin-fa3cd2853530c86c261ac7266ffe4f1726fe9ce6.tar.xz
refactor: Remove unused ParsePrechecks from ParseIntegral
Also: * Remove redundant {} from return statement * Add missing failing c-string test case and "-" and "+" strings * Add missing failing test cases for non-int32_t integral types
Diffstat (limited to 'src')
-rw-r--r--src/test/util_tests.cpp58
-rw-r--r--src/util/strencodings.cpp5
-rw-r--r--src/util/strencodings.h6
3 files changed, 41 insertions, 28 deletions
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
index a13700d733..de78b1bb50 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -1474,6 +1474,35 @@ BOOST_AUTO_TEST_CASE(test_ParseInt32)
BOOST_CHECK(!ParseInt32("32482348723847471234", nullptr));
}
+template <typename T>
+static void RunToIntegralTests()
+{
+ BOOST_CHECK(!ToIntegral<T>(STRING_WITH_EMBEDDED_NULL_CHAR));
+ BOOST_CHECK(!ToIntegral<T>(" 1"));
+ BOOST_CHECK(!ToIntegral<T>("1 "));
+ BOOST_CHECK(!ToIntegral<T>("1a"));
+ BOOST_CHECK(!ToIntegral<T>("1.1"));
+ BOOST_CHECK(!ToIntegral<T>("1.9"));
+ BOOST_CHECK(!ToIntegral<T>("+01.9"));
+ BOOST_CHECK(!ToIntegral<T>("-"));
+ BOOST_CHECK(!ToIntegral<T>("+"));
+ BOOST_CHECK(!ToIntegral<T>(" -1"));
+ BOOST_CHECK(!ToIntegral<T>("-1 "));
+ BOOST_CHECK(!ToIntegral<T>(" -1 "));
+ BOOST_CHECK(!ToIntegral<T>("+1"));
+ BOOST_CHECK(!ToIntegral<T>(" +1"));
+ BOOST_CHECK(!ToIntegral<T>(" +1 "));
+ BOOST_CHECK(!ToIntegral<T>("+-1"));
+ BOOST_CHECK(!ToIntegral<T>("-+1"));
+ BOOST_CHECK(!ToIntegral<T>("++1"));
+ BOOST_CHECK(!ToIntegral<T>("--1"));
+ BOOST_CHECK(!ToIntegral<T>(""));
+ BOOST_CHECK(!ToIntegral<T>("aap"));
+ BOOST_CHECK(!ToIntegral<T>("0x1"));
+ BOOST_CHECK(!ToIntegral<T>("-32482348723847471234"));
+ BOOST_CHECK(!ToIntegral<T>("32482348723847471234"));
+}
+
BOOST_AUTO_TEST_CASE(test_ToIntegral)
{
BOOST_CHECK_EQUAL(ToIntegral<int32_t>("1234").value(), 1'234);
@@ -1486,27 +1515,14 @@ BOOST_AUTO_TEST_CASE(test_ToIntegral)
BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1234").value(), -1'234);
BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1").value(), -1);
- BOOST_CHECK(!ToIntegral<int32_t>(" 1"));
- BOOST_CHECK(!ToIntegral<int32_t>("1 "));
- BOOST_CHECK(!ToIntegral<int32_t>("1a"));
- BOOST_CHECK(!ToIntegral<int32_t>("1.1"));
- BOOST_CHECK(!ToIntegral<int32_t>("1.9"));
- BOOST_CHECK(!ToIntegral<int32_t>("+01.9"));
- BOOST_CHECK(!ToIntegral<int32_t>(" -1"));
- BOOST_CHECK(!ToIntegral<int32_t>("-1 "));
- BOOST_CHECK(!ToIntegral<int32_t>(" -1 "));
- BOOST_CHECK(!ToIntegral<int32_t>("+1"));
- BOOST_CHECK(!ToIntegral<int32_t>(" +1"));
- BOOST_CHECK(!ToIntegral<int32_t>(" +1 "));
- BOOST_CHECK(!ToIntegral<int32_t>("+-1"));
- BOOST_CHECK(!ToIntegral<int32_t>("-+1"));
- BOOST_CHECK(!ToIntegral<int32_t>("++1"));
- BOOST_CHECK(!ToIntegral<int32_t>("--1"));
- BOOST_CHECK(!ToIntegral<int32_t>(""));
- BOOST_CHECK(!ToIntegral<int32_t>("aap"));
- BOOST_CHECK(!ToIntegral<int32_t>("0x1"));
- BOOST_CHECK(!ToIntegral<int32_t>("-32482348723847471234"));
- BOOST_CHECK(!ToIntegral<int32_t>("32482348723847471234"));
+ RunToIntegralTests<uint64_t>();
+ RunToIntegralTests<int64_t>();
+ RunToIntegralTests<uint32_t>();
+ RunToIntegralTests<int32_t>();
+ RunToIntegralTests<uint16_t>();
+ RunToIntegralTests<int16_t>();
+ RunToIntegralTests<uint8_t>();
+ RunToIntegralTests<int8_t>();
BOOST_CHECK(!ToIntegral<int64_t>("-9223372036854775809"));
BOOST_CHECK_EQUAL(ToIntegral<int64_t>("-9223372036854775808").value(), -9'223'372'036'854'775'807LL - 1LL);
diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp
index 0aa80ea0ae..90bf39f010 100644
--- a/src/util/strencodings.cpp
+++ b/src/util/strencodings.cpp
@@ -281,16 +281,11 @@ std::string DecodeBase32(const std::string& str, bool* pf_invalid)
return std::string((const char*)vchRet.data(), vchRet.size());
}
-[[nodiscard]] static bool ParsePrechecks(const std::string&);
-
namespace {
template <typename T>
bool ParseIntegral(const std::string& str, T* out)
{
static_assert(std::is_integral<T>::value);
- if (!ParsePrechecks(str)) {
- return false;
- }
// Replicate the exact behavior of strtol/strtoll/strtoul/strtoull when
// handling leading +/- for backwards compatibility.
if (str.length() >= 2 && str[0] == '+' && str[1] == '-') {
diff --git a/src/util/strencodings.h b/src/util/strencodings.h
index 1217572c45..07e1966890 100644
--- a/src/util/strencodings.h
+++ b/src/util/strencodings.h
@@ -97,7 +97,9 @@ constexpr inline bool IsSpace(char c) noexcept {
}
/**
- * Convert string to integral type T.
+ * Convert string to integral type T. Leading whitespace, a leading +, or any
+ * trailing character fail the parsing. The required format expressed as regex
+ * is `-?[0-9]+`.
*
* @returns std::nullopt if the entire string could not be parsed, or if the
* parsed value is not in the range representable by the type T.
@@ -111,7 +113,7 @@ std::optional<T> ToIntegral(const std::string& str)
if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
return std::nullopt;
}
- return {result};
+ return result;
}
/**