merge bitcoin#23156: Remove unused ParsePrechecks and ParseDouble

This commit is contained in:
Kittywhiskers Van Gogh 2024-10-08 15:55:05 +00:00
parent 18fff7e3d3
commit eb9e20890f
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
4 changed files with 41 additions and 89 deletions

View File

@ -14,9 +14,6 @@ FUZZ_TARGET(parse_numbers)
(void)ParseMoney(random_string); (void)ParseMoney(random_string);
double d;
(void)ParseDouble(random_string, &d);
uint8_t u8; uint8_t u8;
(void)ParseUInt8(random_string, &u8); (void)ParseUInt8(random_string, &u8);

View File

@ -1617,6 +1617,35 @@ BOOST_AUTO_TEST_CASE(test_ParseInt32)
BOOST_CHECK(!ParseInt32("32482348723847471234", nullptr)); 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_AUTO_TEST_CASE(test_ToIntegral)
{ {
BOOST_CHECK_EQUAL(ToIntegral<int32_t>("1234").value(), 1'234); BOOST_CHECK_EQUAL(ToIntegral<int32_t>("1234").value(), 1'234);
@ -1629,27 +1658,14 @@ BOOST_AUTO_TEST_CASE(test_ToIntegral)
BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1234").value(), -1'234); BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1234").value(), -1'234);
BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1").value(), -1); BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1").value(), -1);
BOOST_CHECK(!ToIntegral<int32_t>(" 1")); RunToIntegralTests<uint64_t>();
BOOST_CHECK(!ToIntegral<int32_t>("1 ")); RunToIntegralTests<int64_t>();
BOOST_CHECK(!ToIntegral<int32_t>("1a")); RunToIntegralTests<uint32_t>();
BOOST_CHECK(!ToIntegral<int32_t>("1.1")); RunToIntegralTests<int32_t>();
BOOST_CHECK(!ToIntegral<int32_t>("1.9")); RunToIntegralTests<uint16_t>();
BOOST_CHECK(!ToIntegral<int32_t>("+01.9")); RunToIntegralTests<int16_t>();
BOOST_CHECK(!ToIntegral<int32_t>(" -1")); RunToIntegralTests<uint8_t>();
BOOST_CHECK(!ToIntegral<int32_t>("-1 ")); RunToIntegralTests<int8_t>();
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"));
BOOST_CHECK(!ToIntegral<int64_t>("-9223372036854775809")); BOOST_CHECK(!ToIntegral<int64_t>("-9223372036854775809"));
BOOST_CHECK_EQUAL(ToIntegral<int64_t>("-9223372036854775808").value(), -9'223'372'036'854'775'807LL - 1LL); BOOST_CHECK_EQUAL(ToIntegral<int64_t>("-9223372036854775808").value(), -9'223'372'036'854'775'807LL - 1LL);
@ -1928,32 +1944,6 @@ BOOST_AUTO_TEST_CASE(test_ParseUInt64)
BOOST_CHECK(!ParseUInt64("-1234", &n)); BOOST_CHECK(!ParseUInt64("-1234", &n));
} }
BOOST_AUTO_TEST_CASE(test_ParseDouble)
{
double n;
// Valid values
BOOST_CHECK(ParseDouble("1234", nullptr));
BOOST_CHECK(ParseDouble("0", &n) && n == 0.0);
BOOST_CHECK(ParseDouble("1234", &n) && n == 1234.0);
BOOST_CHECK(ParseDouble("01234", &n) && n == 1234.0); // no octal
BOOST_CHECK(ParseDouble("2147483647", &n) && n == 2147483647.0);
BOOST_CHECK(ParseDouble("-2147483648", &n) && n == -2147483648.0);
BOOST_CHECK(ParseDouble("-1234", &n) && n == -1234.0);
BOOST_CHECK(ParseDouble("1e6", &n) && n == 1e6);
BOOST_CHECK(ParseDouble("-1e6", &n) && n == -1e6);
// Invalid values
BOOST_CHECK(!ParseDouble("", &n));
BOOST_CHECK(!ParseDouble(" 1", &n)); // no padding inside
BOOST_CHECK(!ParseDouble("1 ", &n));
BOOST_CHECK(!ParseDouble("1a", &n));
BOOST_CHECK(!ParseDouble("aap", &n));
BOOST_CHECK(!ParseDouble("0x1", &n)); // no hex
BOOST_CHECK(!ParseDouble(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
// Overflow and underflow
BOOST_CHECK(!ParseDouble("-1e10000", nullptr));
BOOST_CHECK(!ParseDouble("1e10000", nullptr));
}
BOOST_AUTO_TEST_CASE(test_FormatParagraph) BOOST_AUTO_TEST_CASE(test_FormatParagraph)
{ {
BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), ""); BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), "");

View File

@ -260,16 +260,11 @@ std::string DecodeBase32(const std::string& str, bool* pf_invalid)
return std::string((const char*)vchRet.data(), vchRet.size()); return std::string((const char*)vchRet.data(), vchRet.size());
} }
[[nodiscard]] static bool ParsePrechecks(const std::string&);
namespace { namespace {
template <typename T> template <typename T>
bool ParseIntegral(const std::string& str, T* out) bool ParseIntegral(const std::string& str, T* out)
{ {
static_assert(std::is_integral<T>::value); static_assert(std::is_integral<T>::value);
if (!ParsePrechecks(str)) {
return false;
}
// Replicate the exact behavior of strtol/strtoll/strtoul/strtoull when // Replicate the exact behavior of strtol/strtoll/strtoul/strtoull when
// handling leading +/- for backwards compatibility. // handling leading +/- for backwards compatibility.
if (str.length() >= 2 && str[0] == '+' && str[1] == '-') { if (str.length() >= 2 && str[0] == '+' && str[1] == '-') {
@ -286,17 +281,6 @@ bool ParseIntegral(const std::string& str, T* out)
} }
}; // namespace }; // namespace
[[nodiscard]] static bool ParsePrechecks(const std::string& str)
{
if (str.empty()) // No empty string allowed
return false;
if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size()-1]))) // No padding allowed
return false;
if (!ValidAsCString(str)) // No embedded NUL characters allowed
return false;
return true;
}
bool ParseInt32(const std::string& str, int32_t* out) bool ParseInt32(const std::string& str, int32_t* out)
{ {
return ParseIntegral<int32_t>(str, out); return ParseIntegral<int32_t>(str, out);
@ -327,20 +311,6 @@ bool ParseUInt64(const std::string& str, uint64_t* out)
return ParseIntegral<uint64_t>(str, out); return ParseIntegral<uint64_t>(str, out);
} }
bool ParseDouble(const std::string& str, double *out)
{
if (!ParsePrechecks(str))
return false;
if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
return false;
std::istringstream text(str);
text.imbue(std::locale::classic());
double result;
text >> result;
if(out) *out = result;
return text.eof() && !text.fail();
}
std::string FormatParagraph(const std::string& in, size_t width, size_t indent) std::string FormatParagraph(const std::string& in, size_t width, size_t indent)
{ {
assert(width >= indent); assert(width >= indent);

View File

@ -141,7 +141,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 * @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. * parsed value is not in the range representable by the type T.
@ -155,7 +157,7 @@ std::optional<T> ToIntegral(const std::string& str)
if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) { if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
return std::nullopt; return std::nullopt;
} }
return {result}; return result;
} }
/** /**
@ -200,13 +202,6 @@ std::optional<T> ToIntegral(const std::string& str)
*/ */
[[nodiscard]] bool ParseUInt64(const std::string& str, uint64_t *out); [[nodiscard]] bool ParseUInt64(const std::string& str, uint64_t *out);
/**
* Convert string to double with strict parse error feedback.
* @returns true if the entire string could be parsed as valid double,
* false if not the entire string could be parsed or when overflow or underflow occurred.
*/
[[nodiscard]] bool ParseDouble(const std::string& str, double *out);
/** /**
* Convert a span of bytes to a lower-case hexadecimal string. * Convert a span of bytes to a lower-case hexadecimal string.
*/ */