fuzz: [refactor] Use PickValue where possible

This commit is contained in:
MarcoFalke 2021-03-24 07:01:35 +01:00
parent 681c21be9a
commit fa818ca202
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548
3 changed files with 6 additions and 6 deletions

View File

@ -34,7 +34,7 @@ FUZZ_TARGET_INIT(pow, initialize_pow)
} }
CBlockIndex current_block{*block_header}; CBlockIndex current_block{*block_header};
{ {
CBlockIndex* previous_block = !blocks.empty() ? &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)] : nullptr; CBlockIndex* previous_block = blocks.empty() ? nullptr : &PickValue(fuzzed_data_provider, blocks);
const int current_height = (previous_block != nullptr && previous_block->nHeight != std::numeric_limits<int>::max()) ? previous_block->nHeight + 1 : 0; const int current_height = (previous_block != nullptr && previous_block->nHeight != std::numeric_limits<int>::max()) ? previous_block->nHeight + 1 : 0;
if (fuzzed_data_provider.ConsumeBool()) { if (fuzzed_data_provider.ConsumeBool()) {
current_block.pprev = previous_block; current_block.pprev = previous_block;
@ -66,9 +66,9 @@ FUZZ_TARGET_INIT(pow, initialize_pow)
} }
} }
{ {
const CBlockIndex* to = &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)]; const CBlockIndex* to = &PickValue(fuzzed_data_provider, blocks);
const CBlockIndex* from = &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)]; const CBlockIndex* from = &PickValue(fuzzed_data_provider, blocks);
const CBlockIndex* tip = &blocks[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, blocks.size() - 1)]; const CBlockIndex* tip = &PickValue(fuzzed_data_provider, blocks);
try { try {
(void)GetBlockProofEquivalentTime(*to, *from, *tip, consensus_params); (void)GetBlockProofEquivalentTime(*to, *from, *tip, consensus_params);
} catch (const uint_error&) { } catch (const uint_error&) {

View File

@ -65,7 +65,7 @@ FUZZ_TARGET_INIT(process_messages, initialize_process_messages)
net_msg.m_type = random_message_type; net_msg.m_type = random_message_type;
net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider); net_msg.data = ConsumeRandomLengthByteVector(fuzzed_data_provider);
CNode& random_node = *peers.at(fuzzed_data_provider.ConsumeIntegralInRange<int>(0, peers.size() - 1)); CNode& random_node = *PickValue(fuzzed_data_provider, peers);
(void)connman.ReceiveMsgFrom(random_node, net_msg); (void)connman.ReceiveMsgFrom(random_node, net_msg);
random_node.fPauseSend = false; random_node.fPauseSend = false;

View File

@ -49,7 +49,7 @@ void CallOneOf(FuzzedDataProvider& fuzzed_data_provider, Callables... callables)
} }
template <typename Collection> template <typename Collection>
const auto& PickValue(FuzzedDataProvider& fuzzed_data_provider, const Collection& col) auto& PickValue(FuzzedDataProvider& fuzzed_data_provider, Collection& col)
{ {
const auto sz = col.size(); const auto sz = col.size();
assert(sz >= 1); assert(sz >= 1);