Fix crash inside OverlayImpl loops over ids_ (#5071)

This commit is contained in:
Bronek Kozicki
2024-08-02 21:58:05 +01:00
committed by GitHub
parent e5aa605742
commit ffc343a2bc
2 changed files with 9 additions and 2 deletions

View File

@@ -189,7 +189,10 @@ private:
consumer, consumer,
std::move(stream_ptr), std::move(stream_ptr),
overlay); overlay);
BEAST_EXPECT(
overlay.findPeerByPublicKey(key) == std::shared_ptr<PeerImp>{});
overlay.add_active(peer); overlay.add_active(peer);
BEAST_EXPECT(overlay.findPeerByPublicKey(key) == peer);
peers.emplace_back(peer); // overlay stores week ptr to PeerImp peers.emplace_back(peer); // overlay stores week ptr to PeerImp
lid_ += 2; lid_ += 2;
rid_ += 2; rid_ += 2;

View File

@@ -1163,9 +1163,11 @@ OverlayImpl::getActivePeers(
disabled = enabledInSkip = 0; disabled = enabledInSkip = 0;
ret.reserve(ids_.size()); ret.reserve(ids_.size());
// NOTE The purpose of p is to delay the destruction of PeerImp
std::shared_ptr<PeerImp> p;
for (auto& [id, w] : ids_) for (auto& [id, w] : ids_)
{ {
if (auto p = w.lock()) if (p = w.lock(); p != nullptr)
{ {
bool const reduceRelayEnabled = p->txReduceRelayEnabled(); bool const reduceRelayEnabled = p->txReduceRelayEnabled();
// tx reduced relay feature disabled // tx reduced relay feature disabled
@@ -1205,9 +1207,11 @@ std::shared_ptr<Peer>
OverlayImpl::findPeerByPublicKey(PublicKey const& pubKey) OverlayImpl::findPeerByPublicKey(PublicKey const& pubKey)
{ {
std::lock_guard lock(mutex_); std::lock_guard lock(mutex_);
// NOTE The purpose of peer is to delay the destruction of PeerImp
std::shared_ptr<PeerImp> peer;
for (auto const& e : ids_) for (auto const& e : ids_)
{ {
if (auto peer = e.second.lock()) if (peer = e.second.lock(); peer != nullptr)
{ {
if (peer->getNodePublic() == pubKey) if (peer->getNodePublic() == pubKey)
return peer; return peer;