fuzz: Always restrict base conversion input lengths

They seem to cause timeouts:
> Issue 397734700: bitcoin-core:base58check_encode_decode: Timeout in base58check_encode_decode

The `encoded_string.empty()` check was corrected here to `decoded.empty()` to make sure the `(0, decoded.size() - 1)` range is always valid.

Github-Pull: #31917
Rebased-From: bad1433ef2

Co-authored-by: maflcko <6399679+maflcko@users.noreply.github.com>
Co-authored-by: marcofleon <marleo23@proton.me>
Co-authored-by: Martin Zumsande <mzumsande@gmail.com>
This commit is contained in:
Lőrinc 2025-02-20 16:48:28 +01:00 committed by glozow
parent 80c5d57bd1
commit 15ecae31a8

View file

@ -6,6 +6,7 @@
#include <base58.h>
#include <psbt.h>
#include <span.h>
#include <test/fuzz/FuzzedDataProvider.h>
#include <util/strencodings.h>
#include <util/string.h>
@ -19,42 +20,40 @@ using util::TrimStringView;
FUZZ_TARGET(base58_encode_decode)
{
FuzzedDataProvider provider(buffer.data(), buffer.size());
const std::string random_string{provider.ConsumeRandomLengthString(1000)};
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, 1000)};
FuzzedDataProvider provider{buffer.data(), buffer.size()};
const auto random_string{provider.ConsumeRandomLengthString(100)};
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, 100)};
// Decode/Encode roundtrip
std::vector<unsigned char> decoded;
if (DecodeBase58(random_string, decoded, max_ret_len)) {
if (std::vector<unsigned char> decoded; DecodeBase58(random_string, decoded, max_ret_len)) {
const auto encoded_string{EncodeBase58(decoded)};
assert(encoded_string == TrimStringView(random_string));
assert(encoded_string.empty() || !DecodeBase58(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
assert(decoded.empty() || !DecodeBase58(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
}
// Encode/Decode roundtrip
const auto encoded{EncodeBase58(buffer)};
const auto encoded{EncodeBase58(MakeUCharSpan(random_string))};
std::vector<unsigned char> roundtrip_decoded;
assert(DecodeBase58(encoded, roundtrip_decoded, buffer.size())
&& std::ranges::equal(roundtrip_decoded, buffer));
assert(DecodeBase58(encoded, roundtrip_decoded, random_string.size())
&& std::ranges::equal(roundtrip_decoded, MakeUCharSpan(random_string)));
}
FUZZ_TARGET(base58check_encode_decode)
{
FuzzedDataProvider provider(buffer.data(), buffer.size());
const std::string random_string{provider.ConsumeRandomLengthString(1000)};
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, 1000)};
FuzzedDataProvider provider{buffer.data(), buffer.size()};
const auto random_string{provider.ConsumeRandomLengthString(100)};
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, 100)};
// Decode/Encode roundtrip
std::vector<unsigned char> decoded;
if (DecodeBase58Check(random_string, decoded, max_ret_len)) {
if (std::vector<unsigned char> decoded; DecodeBase58Check(random_string, decoded, max_ret_len)) {
const auto encoded_string{EncodeBase58Check(decoded)};
assert(encoded_string == TrimStringView(random_string));
assert(encoded_string.empty() || !DecodeBase58Check(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
assert(decoded.empty() || !DecodeBase58Check(encoded_string, decoded, provider.ConsumeIntegralInRange<int>(0, decoded.size() - 1)));
}
// Encode/Decode roundtrip
const auto encoded{EncodeBase58Check(buffer)};
const auto encoded{EncodeBase58Check(MakeUCharSpan(random_string))};
std::vector<unsigned char> roundtrip_decoded;
assert(DecodeBase58Check(encoded, roundtrip_decoded, buffer.size())
&& std::ranges::equal(roundtrip_decoded, buffer));
assert(DecodeBase58Check(encoded, roundtrip_decoded, random_string.size())
&& std::ranges::equal(roundtrip_decoded, MakeUCharSpan(random_string)));
}
FUZZ_TARGET(base32_encode_decode)