Skip to content

Commit 8d31d76

Browse files
committed
Merge bitcoin/bitcoin#27344: fuzz: Remove legacy int parse fuzz tests
faf8dc4 fuzz: Remove legacy int parse fuzz tests (MarcoFalke) Pull request description: The fuzz tests checked that the result of the new function was equal to the legacy function. (Side note: The checks were incomplete, as evident by the follow-up fix in commit b5c9bb5). Given that they haven't found any issues in years (beside missing the above issue, that they couldn't catch), it seems time to remove them. They may come in handy in the rare case that someone would want to modify `LocaleIndependentAtoi()` or `Parse*Int*()`, however that seems unlikely. Also, appropriate checks can be added then. ACKs for top commit: fanquake: ACK faf8dc4 dergoegge: ACK faf8dc4 Tree-SHA512: 4ec88b9fa8ba49a923b0604016f0f471b3c9b9e0ba6c5c3dc4e20503c6994789921e7221d9ec467a2a37a73f21a70ba51ba3370ed5ad311dee989e218290b29a
2 parents d254f94 + faf8dc4 commit 8d31d76

File tree

3 files changed

+5
-157
lines changed

3 files changed

+5
-157
lines changed

ci/test/06_script_b.sh

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ if [ "${RUN_TIDY}" = "true" ]; then
6161
" src/random.cpp"\
6262
" src/rpc/fees.cpp"\
6363
" src/rpc/signmessage.cpp"\
64+
" src/test/fuzz/string.cpp"\
6465
" src/test/fuzz/txorphan.cpp"\
6566
" src/test/fuzz/util/"\
6667
" src/test/util/coins.cpp"\

src/test/fuzz/string.cpp

+4-155
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include <blockfilter.h>
66
#include <clientversion.h>
77
#include <common/url.h>
8-
#include <logging.h>
9-
#include <netaddress.h>
108
#include <netbase.h>
119
#include <outputtype.h>
1210
#include <rpc/client.h>
@@ -22,113 +20,21 @@
2220
#include <test/fuzz/util.h>
2321
#include <util/error.h>
2422
#include <util/fees.h>
25-
#include <util/message.h>
2623
#include <util/settings.h>
2724
#include <util/strencodings.h>
2825
#include <util/string.h>
2926
#include <util/system.h>
3027
#include <util/translation.h>
31-
#include <version.h>
3228

29+
#include <cassert>
3330
#include <cstdint>
3431
#include <cstdlib>
32+
#include <ios>
33+
#include <stdexcept>
3534
#include <string>
3635
#include <vector>
3736

38-
namespace {
39-
bool LegacyParsePrechecks(const std::string& str)
40-
{
41-
if (str.empty()) // No empty string allowed
42-
return false;
43-
if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size() - 1]))) // No padding allowed
44-
return false;
45-
if (!ContainsNoNUL(str)) // No embedded NUL characters allowed
46-
return false;
47-
return true;
48-
}
49-
50-
bool LegacyParseInt32(const std::string& str, int32_t* out)
51-
{
52-
if (!LegacyParsePrechecks(str))
53-
return false;
54-
char* endp = nullptr;
55-
errno = 0; // strtol will not set errno if valid
56-
long int n = strtol(str.c_str(), &endp, 10);
57-
if (out) *out = (int32_t)n;
58-
// Note that strtol returns a *long int*, so even if strtol doesn't report an over/underflow
59-
// we still have to check that the returned value is within the range of an *int32_t*. On 64-bit
60-
// platforms the size of these types may be different.
61-
return endp && *endp == 0 && !errno &&
62-
n >= std::numeric_limits<int32_t>::min() &&
63-
n <= std::numeric_limits<int32_t>::max();
64-
}
65-
66-
bool LegacyParseInt64(const std::string& str, int64_t* out)
67-
{
68-
if (!LegacyParsePrechecks(str))
69-
return false;
70-
char* endp = nullptr;
71-
errno = 0; // strtoll will not set errno if valid
72-
long long int n = strtoll(str.c_str(), &endp, 10);
73-
if (out) *out = (int64_t)n;
74-
// Note that strtoll returns a *long long int*, so even if strtol doesn't report an over/underflow
75-
// we still have to check that the returned value is within the range of an *int64_t*.
76-
return endp && *endp == 0 && !errno &&
77-
n >= std::numeric_limits<int64_t>::min() &&
78-
n <= std::numeric_limits<int64_t>::max();
79-
}
80-
81-
bool LegacyParseUInt32(const std::string& str, uint32_t* out)
82-
{
83-
if (!LegacyParsePrechecks(str))
84-
return false;
85-
if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoul accepts these by default if they fit in the range
86-
return false;
87-
char* endp = nullptr;
88-
errno = 0; // strtoul will not set errno if valid
89-
unsigned long int n = strtoul(str.c_str(), &endp, 10);
90-
if (out) *out = (uint32_t)n;
91-
// Note that strtoul returns a *unsigned long int*, so even if it doesn't report an over/underflow
92-
// we still have to check that the returned value is within the range of an *uint32_t*. On 64-bit
93-
// platforms the size of these types may be different.
94-
return endp && *endp == 0 && !errno &&
95-
n <= std::numeric_limits<uint32_t>::max();
96-
}
97-
98-
bool LegacyParseUInt8(const std::string& str, uint8_t* out)
99-
{
100-
uint32_t u32;
101-
if (!LegacyParseUInt32(str, &u32) || u32 > std::numeric_limits<uint8_t>::max()) {
102-
return false;
103-
}
104-
if (out != nullptr) {
105-
*out = static_cast<uint8_t>(u32);
106-
}
107-
return true;
108-
}
109-
110-
bool LegacyParseUInt64(const std::string& str, uint64_t* out)
111-
{
112-
if (!LegacyParsePrechecks(str))
113-
return false;
114-
if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoull accepts these by default if they fit in the range
115-
return false;
116-
char* endp = nullptr;
117-
errno = 0; // strtoull will not set errno if valid
118-
unsigned long long int n = strtoull(str.c_str(), &endp, 10);
119-
if (out) *out = (uint64_t)n;
120-
// Note that strtoull returns a *unsigned long long int*, so even if it doesn't report an over/underflow
121-
// we still have to check that the returned value is within the range of an *uint64_t*.
122-
return endp && *endp == 0 && !errno &&
123-
n <= std::numeric_limits<uint64_t>::max();
124-
}
125-
126-
// For backwards compatibility checking.
127-
int64_t atoi64_legacy(const std::string& str)
128-
{
129-
return strtoll(str.c_str(), nullptr, 10);
130-
}
131-
}; // namespace
37+
enum class FeeEstimateMode;
13238

13339
FUZZ_TARGET(string)
13440
{
@@ -236,61 +142,4 @@ FUZZ_TARGET(string)
236142
const bilingual_str bs2{random_string_2, random_string_1};
237143
(void)(bs1 + bs2);
238144
}
239-
{
240-
int32_t i32;
241-
int64_t i64;
242-
uint32_t u32;
243-
uint64_t u64;
244-
uint8_t u8;
245-
const bool ok_i32 = ParseInt32(random_string_1, &i32);
246-
const bool ok_i64 = ParseInt64(random_string_1, &i64);
247-
const bool ok_u32 = ParseUInt32(random_string_1, &u32);
248-
const bool ok_u64 = ParseUInt64(random_string_1, &u64);
249-
const bool ok_u8 = ParseUInt8(random_string_1, &u8);
250-
251-
int32_t i32_legacy;
252-
int64_t i64_legacy;
253-
uint32_t u32_legacy;
254-
uint64_t u64_legacy;
255-
uint8_t u8_legacy;
256-
const bool ok_i32_legacy = LegacyParseInt32(random_string_1, &i32_legacy);
257-
const bool ok_i64_legacy = LegacyParseInt64(random_string_1, &i64_legacy);
258-
const bool ok_u32_legacy = LegacyParseUInt32(random_string_1, &u32_legacy);
259-
const bool ok_u64_legacy = LegacyParseUInt64(random_string_1, &u64_legacy);
260-
const bool ok_u8_legacy = LegacyParseUInt8(random_string_1, &u8_legacy);
261-
262-
assert(ok_i32 == ok_i32_legacy);
263-
assert(ok_i64 == ok_i64_legacy);
264-
assert(ok_u32 == ok_u32_legacy);
265-
assert(ok_u64 == ok_u64_legacy);
266-
assert(ok_u8 == ok_u8_legacy);
267-
268-
if (ok_i32) {
269-
assert(i32 == i32_legacy);
270-
}
271-
if (ok_i64) {
272-
assert(i64 == i64_legacy);
273-
}
274-
if (ok_u32) {
275-
assert(u32 == u32_legacy);
276-
}
277-
if (ok_u64) {
278-
assert(u64 == u64_legacy);
279-
}
280-
if (ok_u8) {
281-
assert(u8 == u8_legacy);
282-
}
283-
}
284-
285-
{
286-
const int locale_independent_atoi_result = LocaleIndependentAtoi<int>(random_string_1);
287-
const int64_t atoi64_result = atoi64_legacy(random_string_1);
288-
assert(locale_independent_atoi_result == std::clamp<int64_t>(atoi64_result, std::numeric_limits<int>::min(), std::numeric_limits<int>::max()));
289-
}
290-
291-
{
292-
const int64_t atoi64_result = atoi64_legacy(random_string_1);
293-
const int64_t locale_independent_atoi_result = LocaleIndependentAtoi<int64_t>(random_string_1);
294-
assert(atoi64_result == locale_independent_atoi_result);
295-
}
296145
}

test/lint/lint-locale-dependence.py

-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@
4444
KNOWN_VIOLATIONS = [
4545
"src/dbwrapper.cpp:.*vsnprintf",
4646
"src/test/fuzz/locale.cpp:.*setlocale",
47-
"src/test/fuzz/string.cpp:.*strtol",
48-
"src/test/fuzz/string.cpp:.*strtoul",
4947
"src/test/util_tests.cpp:.*strtoll",
5048
"src/wallet/bdb.cpp:.*DbEnv::strerror", # False positive
5149
"src/util/syserror.cpp:.*strerror", # Outside this function use `SysErrorString`

0 commit comments

Comments
 (0)