From 9c5775c331e02dab06c78ecbb58488542d16dda7 Mon Sep 17 00:00:00 2001 From: brunoerg Date: Thu, 7 Nov 2024 08:34:16 -0300 Subject: [PATCH] addrman: cap the `max_pct` to not exceed the maximum number of addresses Co-authored-by: Vasil Dimov --- src/addrman.cpp | 2 ++ src/addrman.h | 2 +- src/net.h | 2 +- src/test/fuzz/addrman.cpp | 2 +- src/test/fuzz/connman.cpp | 4 ++-- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index 43e7b6a32ce..9c3a24db900 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -812,9 +812,11 @@ nid_type AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) c std::vector AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct, std::optional network, const bool filtered) const { AssertLockHeld(cs); + Assume(max_pct <= 100); size_t nNodes = vRandom.size(); if (max_pct != 0) { + max_pct = std::min(max_pct, size_t{100}); nNodes = max_pct * nNodes / 100; } if (max_addresses != 0) { diff --git a/src/addrman.h b/src/addrman.h index ba6e13bf97e..2ddf1468624 100644 --- a/src/addrman.h +++ b/src/addrman.h @@ -166,7 +166,7 @@ public: * Return all or many randomly selected addresses, optionally by network. * * @param[in] max_addresses Maximum number of addresses to return (0 = all). - * @param[in] max_pct Maximum percentage of addresses to return (0 = all). + * @param[in] max_pct Maximum percentage of addresses to return (0 = all). Value must be from 0 to 100. * @param[in] network Select only addresses of this network (nullopt = all). * @param[in] filtered Select only addresses that are considered good quality (false = all). * diff --git a/src/net.h b/src/net.h index 6b63e2cc0d1..6f3e6eda7be 100644 --- a/src/net.h +++ b/src/net.h @@ -1152,7 +1152,7 @@ public: * Return all or many randomly selected addresses, optionally by network. * * @param[in] max_addresses Maximum number of addresses to return (0 = all). - * @param[in] max_pct Maximum percentage of addresses to return (0 = all). + * @param[in] max_pct Maximum percentage of addresses to return (0 = all). Value must be from 0 to 100. * @param[in] network Select only addresses of this network (nullopt = all). * @param[in] filtered Select only addresses that are considered high quality (false = all). */ diff --git a/src/test/fuzz/addrman.cpp b/src/test/fuzz/addrman.cpp index a7e7f49d9f4..53dd7215d2e 100644 --- a/src/test/fuzz/addrman.cpp +++ b/src/test/fuzz/addrman.cpp @@ -173,7 +173,7 @@ FUZZ_TARGET(addrman, .init = initialize_addrman) network = fuzzed_data_provider.PickValueInArray(ALL_NETWORKS); } auto max_addresses = fuzzed_data_provider.ConsumeIntegralInRange(0, 4096); - auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange(0, 4096); + auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange(0, 100); auto filtered = fuzzed_data_provider.ConsumeBool(); (void)const_addr_man.GetAddr(max_addresses, max_pct, network, filtered); diff --git a/src/test/fuzz/connman.cpp b/src/test/fuzz/connman.cpp index f2bf44c7613..6e0c3bc236c 100644 --- a/src/test/fuzz/connman.cpp +++ b/src/test/fuzz/connman.cpp @@ -110,13 +110,13 @@ FUZZ_TARGET(connman, .init = initialize_connman) }, [&] { auto max_addresses = fuzzed_data_provider.ConsumeIntegral(); - auto max_pct = fuzzed_data_provider.ConsumeIntegral(); + auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange(0, 100); auto filtered = fuzzed_data_provider.ConsumeBool(); (void)connman.GetAddresses(max_addresses, max_pct, /*network=*/std::nullopt, filtered); }, [&] { auto max_addresses = fuzzed_data_provider.ConsumeIntegral(); - auto max_pct = fuzzed_data_provider.ConsumeIntegral(); + auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange(0, 100); (void)connman.GetAddresses(/*requestor=*/random_node, max_addresses, max_pct); }, [&] {