From 24f72b2045d0856fee425b5a4673333949187c44 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 16 May 2011 16:37:37 +0000 Subject: [PATCH] fix bug in peer list when hitting the upper size limit --- src/policy.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/policy.cpp b/src/policy.cpp index b651acef9..14406ffe3 100644 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -769,6 +769,13 @@ namespace libtorrent if (iter != m_peers.end() && (*iter)->address() == c.remote().address()) found = true; } + // make sure the iterator we got is properly sorted relative + // to the connection's address +// TORRENT_ASSERT(m_peers.empty() +// || (iter == m_peers.end() && (*(iter-1))->address() < c.remote().address()) +// || (iter != m_peers.end() && c.remote().address() < (*iter)->address()) +// || (iter != m_peers.end() && iter != m_peers.begin() && (*(iter-1))->address() < c.remote().address())); + if (found) { i = *iter; @@ -848,12 +855,18 @@ namespace libtorrent if (int(m_peers.size()) >= m_torrent->settings().max_peerlist_size) { + // this may invalidate our iterator! erase_peers(); if (int(m_peers.size()) >= m_torrent->settings().max_peerlist_size) { c.disconnect(errors::too_many_connections); return false; } + // restore it + iter = std::lower_bound( + m_peers.begin(), m_peers.end() + , c.remote().address(), peer_address_compare() + ); } #if TORRENT_USE_IPV6