Skip to content

Commit eb9e208

Browse files
committed
merge bitcoin#23156: Remove unused ParsePrechecks and ParseDouble
1 parent 18fff7e commit eb9e208

File tree

4 files changed

+41
-89
lines changed

4 files changed

+41
-89
lines changed

src/test/fuzz/parse_numbers.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ FUZZ_TARGET(parse_numbers)
1414

1515
(void)ParseMoney(random_string);
1616

17-
double d;
18-
(void)ParseDouble(random_string, &d);
19-
2017
uint8_t u8;
2118
(void)ParseUInt8(random_string, &u8);
2219

src/test/util_tests.cpp

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1617,6 +1617,35 @@ BOOST_AUTO_TEST_CASE(test_ParseInt32)
16171617
BOOST_CHECK(!ParseInt32("32482348723847471234", nullptr));
16181618
}
16191619

1620+
template <typename T>
1621+
static void RunToIntegralTests()
1622+
{
1623+
BOOST_CHECK(!ToIntegral<T>(STRING_WITH_EMBEDDED_NULL_CHAR));
1624+
BOOST_CHECK(!ToIntegral<T>(" 1"));
1625+
BOOST_CHECK(!ToIntegral<T>("1 "));
1626+
BOOST_CHECK(!ToIntegral<T>("1a"));
1627+
BOOST_CHECK(!ToIntegral<T>("1.1"));
1628+
BOOST_CHECK(!ToIntegral<T>("1.9"));
1629+
BOOST_CHECK(!ToIntegral<T>("+01.9"));
1630+
BOOST_CHECK(!ToIntegral<T>("-"));
1631+
BOOST_CHECK(!ToIntegral<T>("+"));
1632+
BOOST_CHECK(!ToIntegral<T>(" -1"));
1633+
BOOST_CHECK(!ToIntegral<T>("-1 "));
1634+
BOOST_CHECK(!ToIntegral<T>(" -1 "));
1635+
BOOST_CHECK(!ToIntegral<T>("+1"));
1636+
BOOST_CHECK(!ToIntegral<T>(" +1"));
1637+
BOOST_CHECK(!ToIntegral<T>(" +1 "));
1638+
BOOST_CHECK(!ToIntegral<T>("+-1"));
1639+
BOOST_CHECK(!ToIntegral<T>("-+1"));
1640+
BOOST_CHECK(!ToIntegral<T>("++1"));
1641+
BOOST_CHECK(!ToIntegral<T>("--1"));
1642+
BOOST_CHECK(!ToIntegral<T>(""));
1643+
BOOST_CHECK(!ToIntegral<T>("aap"));
1644+
BOOST_CHECK(!ToIntegral<T>("0x1"));
1645+
BOOST_CHECK(!ToIntegral<T>("-32482348723847471234"));
1646+
BOOST_CHECK(!ToIntegral<T>("32482348723847471234"));
1647+
}
1648+
16201649
BOOST_AUTO_TEST_CASE(test_ToIntegral)
16211650
{
16221651
BOOST_CHECK_EQUAL(ToIntegral<int32_t>("1234").value(), 1'234);
@@ -1629,27 +1658,14 @@ BOOST_AUTO_TEST_CASE(test_ToIntegral)
16291658
BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1234").value(), -1'234);
16301659
BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1").value(), -1);
16311660

1632-
BOOST_CHECK(!ToIntegral<int32_t>(" 1"));
1633-
BOOST_CHECK(!ToIntegral<int32_t>("1 "));
1634-
BOOST_CHECK(!ToIntegral<int32_t>("1a"));
1635-
BOOST_CHECK(!ToIntegral<int32_t>("1.1"));
1636-
BOOST_CHECK(!ToIntegral<int32_t>("1.9"));
1637-
BOOST_CHECK(!ToIntegral<int32_t>("+01.9"));
1638-
BOOST_CHECK(!ToIntegral<int32_t>(" -1"));
1639-
BOOST_CHECK(!ToIntegral<int32_t>("-1 "));
1640-
BOOST_CHECK(!ToIntegral<int32_t>(" -1 "));
1641-
BOOST_CHECK(!ToIntegral<int32_t>("+1"));
1642-
BOOST_CHECK(!ToIntegral<int32_t>(" +1"));
1643-
BOOST_CHECK(!ToIntegral<int32_t>(" +1 "));
1644-
BOOST_CHECK(!ToIntegral<int32_t>("+-1"));
1645-
BOOST_CHECK(!ToIntegral<int32_t>("-+1"));
1646-
BOOST_CHECK(!ToIntegral<int32_t>("++1"));
1647-
BOOST_CHECK(!ToIntegral<int32_t>("--1"));
1648-
BOOST_CHECK(!ToIntegral<int32_t>(""));
1649-
BOOST_CHECK(!ToIntegral<int32_t>("aap"));
1650-
BOOST_CHECK(!ToIntegral<int32_t>("0x1"));
1651-
BOOST_CHECK(!ToIntegral<int32_t>("-32482348723847471234"));
1652-
BOOST_CHECK(!ToIntegral<int32_t>("32482348723847471234"));
1661+
RunToIntegralTests<uint64_t>();
1662+
RunToIntegralTests<int64_t>();
1663+
RunToIntegralTests<uint32_t>();
1664+
RunToIntegralTests<int32_t>();
1665+
RunToIntegralTests<uint16_t>();
1666+
RunToIntegralTests<int16_t>();
1667+
RunToIntegralTests<uint8_t>();
1668+
RunToIntegralTests<int8_t>();
16531669

16541670
BOOST_CHECK(!ToIntegral<int64_t>("-9223372036854775809"));
16551671
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)
19281944
BOOST_CHECK(!ParseUInt64("-1234", &n));
19291945
}
19301946

1931-
BOOST_AUTO_TEST_CASE(test_ParseDouble)
1932-
{
1933-
double n;
1934-
// Valid values
1935-
BOOST_CHECK(ParseDouble("1234", nullptr));
1936-
BOOST_CHECK(ParseDouble("0", &n) && n == 0.0);
1937-
BOOST_CHECK(ParseDouble("1234", &n) && n == 1234.0);
1938-
BOOST_CHECK(ParseDouble("01234", &n) && n == 1234.0); // no octal
1939-
BOOST_CHECK(ParseDouble("2147483647", &n) && n == 2147483647.0);
1940-
BOOST_CHECK(ParseDouble("-2147483648", &n) && n == -2147483648.0);
1941-
BOOST_CHECK(ParseDouble("-1234", &n) && n == -1234.0);
1942-
BOOST_CHECK(ParseDouble("1e6", &n) && n == 1e6);
1943-
BOOST_CHECK(ParseDouble("-1e6", &n) && n == -1e6);
1944-
// Invalid values
1945-
BOOST_CHECK(!ParseDouble("", &n));
1946-
BOOST_CHECK(!ParseDouble(" 1", &n)); // no padding inside
1947-
BOOST_CHECK(!ParseDouble("1 ", &n));
1948-
BOOST_CHECK(!ParseDouble("1a", &n));
1949-
BOOST_CHECK(!ParseDouble("aap", &n));
1950-
BOOST_CHECK(!ParseDouble("0x1", &n)); // no hex
1951-
BOOST_CHECK(!ParseDouble(STRING_WITH_EMBEDDED_NULL_CHAR, &n));
1952-
// Overflow and underflow
1953-
BOOST_CHECK(!ParseDouble("-1e10000", nullptr));
1954-
BOOST_CHECK(!ParseDouble("1e10000", nullptr));
1955-
}
1956-
19571947
BOOST_AUTO_TEST_CASE(test_FormatParagraph)
19581948
{
19591949
BOOST_CHECK_EQUAL(FormatParagraph("", 79, 0), "");

src/util/strencodings.cpp

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,11 @@ std::string DecodeBase32(const std::string& str, bool* pf_invalid)
260260
return std::string((const char*)vchRet.data(), vchRet.size());
261261
}
262262

263-
[[nodiscard]] static bool ParsePrechecks(const std::string&);
264-
265263
namespace {
266264
template <typename T>
267265
bool ParseIntegral(const std::string& str, T* out)
268266
{
269267
static_assert(std::is_integral<T>::value);
270-
if (!ParsePrechecks(str)) {
271-
return false;
272-
}
273268
// Replicate the exact behavior of strtol/strtoll/strtoul/strtoull when
274269
// handling leading +/- for backwards compatibility.
275270
if (str.length() >= 2 && str[0] == '+' && str[1] == '-') {
@@ -286,17 +281,6 @@ bool ParseIntegral(const std::string& str, T* out)
286281
}
287282
}; // namespace
288283

289-
[[nodiscard]] static bool ParsePrechecks(const std::string& str)
290-
{
291-
if (str.empty()) // No empty string allowed
292-
return false;
293-
if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size()-1]))) // No padding allowed
294-
return false;
295-
if (!ValidAsCString(str)) // No embedded NUL characters allowed
296-
return false;
297-
return true;
298-
}
299-
300284
bool ParseInt32(const std::string& str, int32_t* out)
301285
{
302286
return ParseIntegral<int32_t>(str, out);
@@ -327,20 +311,6 @@ bool ParseUInt64(const std::string& str, uint64_t* out)
327311
return ParseIntegral<uint64_t>(str, out);
328312
}
329313

330-
bool ParseDouble(const std::string& str, double *out)
331-
{
332-
if (!ParsePrechecks(str))
333-
return false;
334-
if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed
335-
return false;
336-
std::istringstream text(str);
337-
text.imbue(std::locale::classic());
338-
double result;
339-
text >> result;
340-
if(out) *out = result;
341-
return text.eof() && !text.fail();
342-
}
343-
344314
std::string FormatParagraph(const std::string& in, size_t width, size_t indent)
345315
{
346316
assert(width >= indent);

src/util/strencodings.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ constexpr inline bool IsSpace(char c) noexcept {
141141
}
142142

143143
/**
144-
* Convert string to integral type T.
144+
* Convert string to integral type T. Leading whitespace, a leading +, or any
145+
* trailing character fail the parsing. The required format expressed as regex
146+
* is `-?[0-9]+`.
145147
*
146148
* @returns std::nullopt if the entire string could not be parsed, or if the
147149
* parsed value is not in the range representable by the type T.
@@ -155,7 +157,7 @@ std::optional<T> ToIntegral(const std::string& str)
155157
if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
156158
return std::nullopt;
157159
}
158-
return {result};
160+
return result;
159161
}
160162

161163
/**
@@ -200,13 +202,6 @@ std::optional<T> ToIntegral(const std::string& str)
200202
*/
201203
[[nodiscard]] bool ParseUInt64(const std::string& str, uint64_t *out);
202204

203-
/**
204-
* Convert string to double with strict parse error feedback.
205-
* @returns true if the entire string could be parsed as valid double,
206-
* false if not the entire string could be parsed or when overflow or underflow occurred.
207-
*/
208-
[[nodiscard]] bool ParseDouble(const std::string& str, double *out);
209-
210205
/**
211206
* Convert a span of bytes to a lower-case hexadecimal string.
212207
*/

0 commit comments

Comments
 (0)