diff --git a/src/addrman.cpp b/src/addrman.cpp index 43e7b6a32c..9c3a24db90 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 ba6e13bf97..2ddf146862 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 20c654c34a..fc096ff7b8 100644 --- a/src/net.h +++ b/src/net.h @@ -1155,7 +1155,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 a7e7f49d9f..53dd7215d2 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 27e56f22be..20a60b098f 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); }, [&] {