Browse Source

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

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

12
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
{ {
CryptoPP::RandomNumberGenerator& rnd = i2p::context.GetRandomNumberGenerator (); if(m_Peers.empty()) // ensure m.Peers.size() >= 1
auto it = m_Peers.begin (); return nullptr;
std::advance (it, rnd.GenerateWord32 (0, m_Peers.size () - 1));
return it != m_Peers.end () ? it->second.router : 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;
} }
} }
} }

Loading…
Cancel
Save