Skip to content

Commit 11f3bc2

Browse files
committed
refactor: Reserve vectors in fuzz tests
* Since the main LIMITED_WHILE stated `outpoints.size() < 200'000`, I've presized outpoints accordingly. * `tx_mut.vin` and `tx_mut.vout` weren't caught by the clang-tidy, but addressed them anyway.
1 parent 152fefe commit 11f3bc2

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

src/test/fuzz/txorphan.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
3838

3939
TxOrphanage orphanage;
4040
std::vector<COutPoint> outpoints; // Duplicates are tolerated
41+
outpoints.reserve(200'000);
4142

4243
// initial outpoints used to construct transactions later
4344
for (uint8_t i = 0; i < 4; i++) {
@@ -55,12 +56,14 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
5556
const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, 256);
5657
// pick outpoints from outpoints as input. We allow input duplicates on purpose, given we are not
5758
// running any transaction validation logic before adding transactions to the orphanage
59+
tx_mut.vin.reserve(num_in);
5860
for (uint32_t i = 0; i < num_in; i++) {
5961
auto& prevout = PickValue(fuzzed_data_provider, outpoints);
6062
// try making transactions unique by setting a random nSequence, but allow duplicate transactions if they happen
6163
tx_mut.vin.emplace_back(prevout, CScript{}, fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, CTxIn::SEQUENCE_FINAL));
6264
}
6365
// output amount will not affect txorphanage
66+
tx_mut.vout.reserve(num_out);
6467
for (uint32_t i = 0; i < num_out; i++) {
6568
tx_mut.vout.emplace_back(CAmount{0}, CScript{});
6669
}

src/test/fuzz/util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ template<typename B = uint8_t>
7979
{
8080
const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
8181
std::vector<std::string> r;
82+
r.reserve(n_elements);
8283
for (size_t i = 0; i < n_elements; ++i) {
8384
r.push_back(fuzzed_data_provider.ConsumeRandomLengthString(max_string_length));
8485
}
@@ -90,6 +91,7 @@ template <typename T>
9091
{
9192
const size_t n_elements = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, max_vector_size);
9293
std::vector<T> r;
94+
r.reserve(n_elements);
9395
for (size_t i = 0; i < n_elements; ++i) {
9496
r.push_back(fuzzed_data_provider.ConsumeIntegral<T>());
9597
}

0 commit comments

Comments
 (0)