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

This commit is contained in:
EinMByte 2015-07-22 12:11:21 +02:00
parent 28969bfeab
commit d0b2a0da14

View File

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