Skip to content

Commit dc81dd0

Browse files
committed
Return false early if vEvictionCandidates is empty
1 parent 17f3533 commit dc81dd0

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/net.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,8 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) {
846846
}
847847
}
848848

849+
if (vEvictionCandidates.empty()) return false;
850+
849851
// Protect connections with certain characteristics
850852

851853
// Deterministically select 4 peers to protect by netgroup.
@@ -854,18 +856,21 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) {
854856
std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), comparerNetGroupKeyed);
855857
vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(4, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
856858

859+
if (vEvictionCandidates.empty()) return false;
860+
857861
// Protect the 8 nodes with the best ping times.
858862
// An attacker cannot manipulate this metric without physically moving nodes closer to the target.
859863
std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeMinPingTime);
860864
vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(8, static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
861865

866+
if (vEvictionCandidates.empty()) return false;
867+
862868
// Protect the 64 nodes which have been connected the longest.
863869
// This replicates the existing implicit behavior.
864870
std::sort(vEvictionCandidates.begin(), vEvictionCandidates.end(), ReverseCompareNodeTimeConnected);
865-
vEvictionCandidates.erase(vEvictionCandidates.end() - std::min(static_cast<int>(vEvictionCandidates.size() / 2), static_cast<int>(vEvictionCandidates.size())), vEvictionCandidates.end());
871+
vEvictionCandidates.erase(vEvictionCandidates.end() - static_cast<int>(vEvictionCandidates.size() / 2), vEvictionCandidates.end());
866872

867-
if (vEvictionCandidates.empty())
868-
return false;
873+
if (vEvictionCandidates.empty()) return false;
869874

870875
// Identify CNetAddr with the most connections
871876
CNetAddr naMostConnections;

0 commit comments

Comments
 (0)