Skip to content

Commit fab646b

Browse files
author
MarcoFalke
committed
fuzz: Use correct variant of ConsumeRandomLengthString instead of hardcoding a maximum size
This is technically a breaking change. This allows the fuzz engine to pick the right size, also larger sizes, if needed.
1 parent fae2c8b commit fab646b

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/test/fuzz/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ CScriptWitness ConsumeScriptWitness(FuzzedDataProvider& fuzzed_data_provider, co
276276
return ret;
277277
}
278278

279-
CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length, const bool maybe_p2wsh) noexcept
279+
CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length, const bool maybe_p2wsh) noexcept
280280
{
281281
const std::vector<uint8_t> b = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
282282
CScript r_script{b.begin(), b.end()};

src/test/fuzz/util.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,20 @@ auto& PickValue(FuzzedDataProvider& fuzzed_data_provider, Collection& col)
5757
return *it;
5858
}
5959

60-
[[nodiscard]] inline std::vector<uint8_t> ConsumeRandomLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length = 4096) noexcept
60+
[[nodiscard]] inline std::vector<uint8_t> ConsumeRandomLengthByteVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
6161
{
62-
const std::string s = fuzzed_data_provider.ConsumeRandomLengthString(max_length);
62+
const std::string s = max_length ?
63+
fuzzed_data_provider.ConsumeRandomLengthString(*max_length) :
64+
fuzzed_data_provider.ConsumeRandomLengthString();
6365
return {s.begin(), s.end()};
6466
}
6567

66-
[[nodiscard]] inline std::vector<bool> ConsumeRandomLengthBitVector(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length = 4096) noexcept
68+
[[nodiscard]] inline std::vector<bool> ConsumeRandomLengthBitVector(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
6769
{
6870
return BytesToBits(ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length));
6971
}
7072

71-
[[nodiscard]] inline CDataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length = 4096) noexcept
73+
[[nodiscard]] inline CDataStream ConsumeDataStream(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
7274
{
7375
return CDataStream{ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length), SER_NETWORK, INIT_PROTO_VERSION};
7476
}
@@ -95,7 +97,7 @@ template <typename T>
9597
}
9698

9799
template <typename T>
98-
[[nodiscard]] inline std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length = 4096) noexcept
100+
[[nodiscard]] inline std::optional<T> ConsumeDeserializable(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt) noexcept
99101
{
100102
const std::vector<uint8_t> buffer = ConsumeRandomLengthByteVector(fuzzed_data_provider, max_length);
101103
CDataStream ds{buffer, SER_NETWORK, INIT_PROTO_VERSION};
@@ -132,7 +134,7 @@ template <typename WeakEnumType, size_t size>
132134

133135
[[nodiscard]] CScriptWitness ConsumeScriptWitness(FuzzedDataProvider& fuzzed_data_provider, const size_t max_stack_elem_size = 32) noexcept;
134136

135-
[[nodiscard]] CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider, const size_t max_length = 4096, const bool maybe_p2wsh = false) noexcept;
137+
[[nodiscard]] CScript ConsumeScript(FuzzedDataProvider& fuzzed_data_provider, const std::optional<size_t>& max_length = std::nullopt, const bool maybe_p2wsh = false) noexcept;
136138

137139
[[nodiscard]] uint32_t ConsumeSequence(FuzzedDataProvider& fuzzed_data_provider) noexcept;
138140

0 commit comments

Comments
 (0)