Browse Source

Fix #227: Transports::GetRandomPeer() crashes if m_Peers is empty

pull/228/head
EinMByte 9 years ago
parent
commit
d0b2a0da14
  1. 6
      Transports.cpp

6
Transports.cpp

@ -491,10 +491,14 @@ namespace transport
std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRandomPeer () const std::shared_ptr<const i2p::data::RouterInfo> Transports::GetRandomPeer () const
{ {
if(m_Peers.empty()) // ensure m.Peers.size() >= 1
return nullptr;
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator(); CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator();
auto it = m_Peers.begin(); auto it = m_Peers.begin();
std::advance(it, rnd.GenerateWord32(0, m_Peers.size () - 1)); std::advance(it, rnd.GenerateWord32(0, m_Peers.size () - 1));
return it != m_Peers.end () ? it->second.router : nullptr;
return it->second.router;
} }
} }
} }

Loading…
Cancel
Save