mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
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. 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:
parent
5b8fd7c3a6
commit
bad1433ef2
1 changed files with 17 additions and 18 deletions
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include <base58.h>
|
#include <base58.h>
|
||||||
#include <psbt.h>
|
#include <psbt.h>
|
||||||
|
#include <span.h>
|
||||||
#include <test/fuzz/FuzzedDataProvider.h>
|
#include <test/fuzz/FuzzedDataProvider.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <util/string.h>
|
#include <util/string.h>
|
||||||
|
@ -19,42 +20,40 @@ using util::TrimStringView;
|
||||||
|
|
||||||
FUZZ_TARGET(base58_encode_decode)
|
FUZZ_TARGET(base58_encode_decode)
|
||||||
{
|
{
|
||||||
FuzzedDataProvider provider(buffer.data(), buffer.size());
|
FuzzedDataProvider provider{buffer.data(), buffer.size()};
|
||||||
const std::string random_string{provider.ConsumeRandomLengthString(1000)};
|
const auto random_string{provider.ConsumeRandomLengthString(100)};
|
||||||
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, 1000)};
|
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, 100)};
|
||||||
|
|
||||||
// Decode/Encode roundtrip
|
// Decode/Encode roundtrip
|
||||||
std::vector<unsigned char> decoded;
|
if (std::vector<unsigned char> decoded; DecodeBase58(random_string, decoded, max_ret_len)) {
|
||||||
if (DecodeBase58(random_string, decoded, max_ret_len)) {
|
|
||||||
const auto encoded_string{EncodeBase58(decoded)};
|
const auto encoded_string{EncodeBase58(decoded)};
|
||||||
assert(encoded_string == TrimStringView(random_string));
|
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
|
// Encode/Decode roundtrip
|
||||||
const auto encoded{EncodeBase58(buffer)};
|
const auto encoded{EncodeBase58(MakeUCharSpan(random_string))};
|
||||||
std::vector<unsigned char> roundtrip_decoded;
|
std::vector<unsigned char> roundtrip_decoded;
|
||||||
assert(DecodeBase58(encoded, roundtrip_decoded, buffer.size())
|
assert(DecodeBase58(encoded, roundtrip_decoded, random_string.size())
|
||||||
&& std::ranges::equal(roundtrip_decoded, buffer));
|
&& std::ranges::equal(roundtrip_decoded, MakeUCharSpan(random_string)));
|
||||||
}
|
}
|
||||||
|
|
||||||
FUZZ_TARGET(base58check_encode_decode)
|
FUZZ_TARGET(base58check_encode_decode)
|
||||||
{
|
{
|
||||||
FuzzedDataProvider provider(buffer.data(), buffer.size());
|
FuzzedDataProvider provider{buffer.data(), buffer.size()};
|
||||||
const std::string random_string{provider.ConsumeRandomLengthString(1000)};
|
const auto random_string{provider.ConsumeRandomLengthString(100)};
|
||||||
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, 1000)};
|
const int max_ret_len{provider.ConsumeIntegralInRange<int>(-1, 100)};
|
||||||
|
|
||||||
// Decode/Encode roundtrip
|
// Decode/Encode roundtrip
|
||||||
std::vector<unsigned char> decoded;
|
if (std::vector<unsigned char> decoded; DecodeBase58Check(random_string, decoded, max_ret_len)) {
|
||||||
if (DecodeBase58Check(random_string, decoded, max_ret_len)) {
|
|
||||||
const auto encoded_string{EncodeBase58Check(decoded)};
|
const auto encoded_string{EncodeBase58Check(decoded)};
|
||||||
assert(encoded_string == TrimStringView(random_string));
|
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
|
// Encode/Decode roundtrip
|
||||||
const auto encoded{EncodeBase58Check(buffer)};
|
const auto encoded{EncodeBase58Check(MakeUCharSpan(random_string))};
|
||||||
std::vector<unsigned char> roundtrip_decoded;
|
std::vector<unsigned char> roundtrip_decoded;
|
||||||
assert(DecodeBase58Check(encoded, roundtrip_decoded, buffer.size())
|
assert(DecodeBase58Check(encoded, roundtrip_decoded, random_string.size())
|
||||||
&& std::ranges::equal(roundtrip_decoded, buffer));
|
&& std::ranges::equal(roundtrip_decoded, MakeUCharSpan(random_string)));
|
||||||
}
|
}
|
||||||
|
|
||||||
FUZZ_TARGET(base32_encode_decode)
|
FUZZ_TARGET(base32_encode_decode)
|
||||||
|
|
Loading…
Add table
Reference in a new issue