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 tequ
parent 6a17c6be3f
commit b51411f728
2 changed files with 9 additions and 2 deletions

View File

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

View File

@@ -1150,9 +1150,11 @@ OverlayImpl::getActivePeers(
disabled = enabledInSkip = 0;
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_)
{
if (auto p = w.lock())
if (p = w.lock(); p != nullptr)
{
bool const reduceRelayEnabled = p->txReduceRelayEnabled();
// tx reduced relay feature disabled
@@ -1192,9 +1194,11 @@ std::shared_ptr<Peer>
OverlayImpl::findPeerByPublicKey(PublicKey const& pubKey)
{
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_)
{
if (auto peer = e.second.lock())
if (peer = e.second.lock(); peer != nullptr)
{
if (peer->getNodePublic() == pubKey)
return peer;