aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorbrunoerg <brunoely.gc@gmail.com>2024-11-07 08:34:16 -0300
committerbrunoerg <brunoely.gc@gmail.com>2024-11-11 12:47:53 -0300
commit9c5775c331e02dab06c78ecbb58488542d16dda7 (patch)
tree270a37db97675100ed3f0d1b1b22224e987d91fd /src
parent2c90f8e08c4cf44d4c1ef3dde0e7f7991b8b9390 (diff)
addrman: cap the `max_pct` to not exceed the maximum number of addresses
Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
Diffstat (limited to 'src')
-rw-r--r--src/addrman.cpp2
-rw-r--r--src/addrman.h2
-rw-r--r--src/net.h2
-rw-r--r--src/test/fuzz/addrman.cpp2
-rw-r--r--src/test/fuzz/connman.cpp4
5 files changed, 7 insertions, 5 deletions
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<CAddress> AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct, std::optional<Network> 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 6b63e2cc0d..6f3e6eda7b 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 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<size_t>(0, 4096);
- auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 4096);
+ auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(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 f2bf44c761..6e0c3bc236 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<size_t>();
- auto max_pct = fuzzed_data_provider.ConsumeIntegral<size_t>();
+ auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(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<size_t>();
- auto max_pct = fuzzed_data_provider.ConsumeIntegral<size_t>();
+ auto max_pct = fuzzed_data_provider.ConsumeIntegralInRange<size_t>(0, 100);
(void)connman.GetAddresses(/*requestor=*/random_node, max_addresses, max_pct);
},
[&] {