Skip to content

Commit fa121f0

Browse files
author
MarcoFalke
committed
fuzz: Use ConsumeNode in process_messages target
1 parent 6f97172 commit fa121f0

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/test/fuzz/process_messages.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,7 @@ FUZZ_TARGET_INIT(process_messages, initialize_process_messages)
4747

4848
const auto num_peers_to_add = fuzzed_data_provider.ConsumeIntegralInRange(1, 3);
4949
for (int i = 0; i < num_peers_to_add; ++i) {
50-
const ServiceFlags service_flags = ServiceFlags(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
51-
const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray({ConnectionType::INBOUND, ConnectionType::OUTBOUND_FULL_RELAY, ConnectionType::MANUAL, ConnectionType::FEELER, ConnectionType::BLOCK_RELAY, ConnectionType::ADDR_FETCH});
52-
peers.push_back(MakeUnique<CNode>(i, service_flags, INVALID_SOCKET, CAddress{CService{in_addr{0x0100007f}, 7777}, NODE_NETWORK}, 0, 0, CAddress{}, std::string{}, conn_type).release());
50+
peers.push_back(ConsumeNodeAsUniquePtr(fuzzed_data_provider, i).release());
5351
CNode& p2p_node = *peers.back();
5452

5553
p2p_node.fSuccessfullyConnected = true;

src/test/fuzz/util.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,9 +286,10 @@ inline CAddress ConsumeAddress(FuzzedDataProvider& fuzzed_data_provider) noexcep
286286
return {ConsumeService(fuzzed_data_provider), static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>()), fuzzed_data_provider.ConsumeIntegral<uint32_t>()};
287287
}
288288

289-
inline CNode ConsumeNode(FuzzedDataProvider& fuzzed_data_provider) noexcept
289+
template <bool ReturnUniquePtr = false>
290+
auto ConsumeNode(FuzzedDataProvider& fuzzed_data_provider, const std::optional<NodeId>& node_id_in = nullopt) noexcept
290291
{
291-
const NodeId node_id = fuzzed_data_provider.ConsumeIntegral<NodeId>();
292+
const NodeId node_id = node_id_in.value_or(fuzzed_data_provider.ConsumeIntegral<NodeId>());
292293
const ServiceFlags local_services = static_cast<ServiceFlags>(fuzzed_data_provider.ConsumeIntegral<uint64_t>());
293294
const SOCKET socket = INVALID_SOCKET;
294295
const CAddress address = ConsumeAddress(fuzzed_data_provider);
@@ -298,8 +299,13 @@ inline CNode ConsumeNode(FuzzedDataProvider& fuzzed_data_provider) noexcept
298299
const std::string addr_name = fuzzed_data_provider.ConsumeRandomLengthString(64);
299300
const ConnectionType conn_type = fuzzed_data_provider.PickValueInArray({ConnectionType::INBOUND, ConnectionType::OUTBOUND_FULL_RELAY, ConnectionType::MANUAL, ConnectionType::FEELER, ConnectionType::BLOCK_RELAY, ConnectionType::ADDR_FETCH});
300301
const bool inbound_onion{conn_type == ConnectionType::INBOUND ? fuzzed_data_provider.ConsumeBool() : false};
301-
return {node_id, local_services, socket, address, keyed_net_group, local_host_nonce, addr_bind, addr_name, conn_type, inbound_onion};
302+
if constexpr (ReturnUniquePtr) {
303+
return std::make_unique<CNode>(node_id, local_services, socket, address, keyed_net_group, local_host_nonce, addr_bind, addr_name, conn_type, inbound_onion);
304+
} else {
305+
return CNode{node_id, local_services, socket, address, keyed_net_group, local_host_nonce, addr_bind, addr_name, conn_type, inbound_onion};
306+
}
302307
}
308+
inline std::unique_ptr<CNode> ConsumeNodeAsUniquePtr(FuzzedDataProvider& fdp, const std::optional<NodeId>& node_id_in = nullopt) { return ConsumeNode<true>(fdp, node_id_in); }
303309

304310
inline void InitializeFuzzingContext(const std::string& chain_name = CBaseChainParams::REGTEST)
305311
{

0 commit comments

Comments
 (0)