From b9f1e86f129e46bb5770fb421d0ba164b5c7aaf8 Mon Sep 17 00:00:00 2001 From: Amiti Uttarwar Date: Tue, 25 Apr 2023 17:04:11 +0100 Subject: [PATCH] addrman: change asserts to Assumes `Assume` is safer since the checks are non-fatal- errors in these functions should provide feedback in debug builds, but do not need to deter further node operations in production. --- src/addrman.cpp | 18 +++++++++--------- src/addrman_impl.h | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/addrman.cpp b/src/addrman.cpp index 30ce2cadc8..19a4f4fa63 100644 --- a/src/addrman.cpp +++ b/src/addrman.cpp @@ -764,9 +764,7 @@ std::pair AddrManImpl::Select_(bool new_only, std::option if (node_id != -1) { if (network.has_value()) { const auto it{mapInfo.find(node_id)}; - assert(it != mapInfo.end()); - const auto info{it->second}; - if (info.GetNetwork() == *network) break; + if (Assume(it != mapInfo.end()) && it->second.GetNetwork() == *network) break; } else { break; } @@ -796,15 +794,17 @@ int AddrManImpl::GetEntry(bool use_tried, size_t bucket, size_t position) const { AssertLockHeld(cs); - assert(position < ADDRMAN_BUCKET_SIZE); - if (use_tried) { - assert(bucket < ADDRMAN_TRIED_BUCKET_COUNT); - return vvTried[bucket][position]; + if (Assume(position < ADDRMAN_BUCKET_SIZE) && Assume(bucket < ADDRMAN_TRIED_BUCKET_COUNT)) { + return vvTried[bucket][position]; + } } else { - assert(bucket < ADDRMAN_NEW_BUCKET_COUNT); - return vvNew[bucket][position]; + if (Assume(position < ADDRMAN_BUCKET_SIZE) && Assume(bucket < ADDRMAN_NEW_BUCKET_COUNT)) { + return vvNew[bucket][position]; + } } + + return -1; } std::vector AddrManImpl::GetAddr_(size_t max_addresses, size_t max_pct, std::optional network) const diff --git a/src/addrman_impl.h b/src/addrman_impl.h index 7aead2812b..9aff408e34 100644 --- a/src/addrman_impl.h +++ b/src/addrman_impl.h @@ -255,7 +255,7 @@ private: /** Helper to generalize looking up an addrman entry from either table. * - * @return int The nid of the entry or -1 if the addrman position is empty. + * @return int The nid of the entry. If the addrman position is empty or not found, returns -1. * */ int GetEntry(bool use_tried, size_t bucket, size_t position) const EXCLUSIVE_LOCKS_REQUIRED(cs);