From fa3cd2853530c86c261ac7266ffe4f1726fe9ce6 Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Fri, 1 Oct 2021 17:33:35 +0200 Subject: [PATCH] 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 --- src/test/util_tests.cpp | 58 +++++++++++++++++++++++++-------------- src/util/strencodings.cpp | 5 ---- src/util/strencodings.h | 6 ++-- 3 files changed, 41 insertions(+), 28 deletions(-) diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index a13700d733b..de78b1bb509 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 +static void RunToIntegralTests() +{ + BOOST_CHECK(!ToIntegral(STRING_WITH_EMBEDDED_NULL_CHAR)); + BOOST_CHECK(!ToIntegral(" 1")); + BOOST_CHECK(!ToIntegral("1 ")); + BOOST_CHECK(!ToIntegral("1a")); + BOOST_CHECK(!ToIntegral("1.1")); + BOOST_CHECK(!ToIntegral("1.9")); + BOOST_CHECK(!ToIntegral("+01.9")); + BOOST_CHECK(!ToIntegral("-")); + BOOST_CHECK(!ToIntegral("+")); + BOOST_CHECK(!ToIntegral(" -1")); + BOOST_CHECK(!ToIntegral("-1 ")); + BOOST_CHECK(!ToIntegral(" -1 ")); + BOOST_CHECK(!ToIntegral("+1")); + BOOST_CHECK(!ToIntegral(" +1")); + BOOST_CHECK(!ToIntegral(" +1 ")); + BOOST_CHECK(!ToIntegral("+-1")); + BOOST_CHECK(!ToIntegral("-+1")); + BOOST_CHECK(!ToIntegral("++1")); + BOOST_CHECK(!ToIntegral("--1")); + BOOST_CHECK(!ToIntegral("")); + BOOST_CHECK(!ToIntegral("aap")); + BOOST_CHECK(!ToIntegral("0x1")); + BOOST_CHECK(!ToIntegral("-32482348723847471234")); + BOOST_CHECK(!ToIntegral("32482348723847471234")); +} + BOOST_AUTO_TEST_CASE(test_ToIntegral) { BOOST_CHECK_EQUAL(ToIntegral("1234").value(), 1'234); @@ -1486,27 +1515,14 @@ BOOST_AUTO_TEST_CASE(test_ToIntegral) BOOST_CHECK_EQUAL(ToIntegral("-1234").value(), -1'234); BOOST_CHECK_EQUAL(ToIntegral("-1").value(), -1); - BOOST_CHECK(!ToIntegral(" 1")); - BOOST_CHECK(!ToIntegral("1 ")); - BOOST_CHECK(!ToIntegral("1a")); - BOOST_CHECK(!ToIntegral("1.1")); - BOOST_CHECK(!ToIntegral("1.9")); - BOOST_CHECK(!ToIntegral("+01.9")); - BOOST_CHECK(!ToIntegral(" -1")); - BOOST_CHECK(!ToIntegral("-1 ")); - BOOST_CHECK(!ToIntegral(" -1 ")); - BOOST_CHECK(!ToIntegral("+1")); - BOOST_CHECK(!ToIntegral(" +1")); - BOOST_CHECK(!ToIntegral(" +1 ")); - BOOST_CHECK(!ToIntegral("+-1")); - BOOST_CHECK(!ToIntegral("-+1")); - BOOST_CHECK(!ToIntegral("++1")); - BOOST_CHECK(!ToIntegral("--1")); - BOOST_CHECK(!ToIntegral("")); - BOOST_CHECK(!ToIntegral("aap")); - BOOST_CHECK(!ToIntegral("0x1")); - BOOST_CHECK(!ToIntegral("-32482348723847471234")); - BOOST_CHECK(!ToIntegral("32482348723847471234")); + RunToIntegralTests(); + RunToIntegralTests(); + RunToIntegralTests(); + RunToIntegralTests(); + RunToIntegralTests(); + RunToIntegralTests(); + RunToIntegralTests(); + RunToIntegralTests(); BOOST_CHECK(!ToIntegral("-9223372036854775809")); BOOST_CHECK_EQUAL(ToIntegral("-9223372036854775808").value(), -9'223'372'036'854'775'807LL - 1LL); diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp index 0aa80ea0aed..90bf39f0100 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 bool ParseIntegral(const std::string& str, T* out) { static_assert(std::is_integral::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 1217572c451..07e19668902 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 ToIntegral(const std::string& str) if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) { return std::nullopt; } - return {result}; + return result; } /**