Skip to content

Commit

Permalink
Fix crash inside OverlayImpl loops over ids_ (XRPLF#5071)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bronek authored Aug 2, 2024
1 parent e5aa605 commit ffc343a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/test/overlay/tx_reduce_relay_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ class tx_reduce_relay_test : public beast::unit_test::suite
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;
Expand Down
8 changes: 6 additions & 2 deletions src/xrpld/overlay/detail/OverlayImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1163,9 +1163,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
Expand Down Expand Up @@ -1205,9 +1207,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;
Expand Down

0 comments on commit ffc343a

Please sign in to comment.