From 8a47c849cef0e19465080733ad34e0727ed3f531 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 27 Dec 2007 20:57:58 +0000 Subject: [PATCH] fix in the smart_ban to not use invalid pointers --- include/libtorrent/policy.hpp | 2 ++ src/policy.cpp | 11 +++++++++++ src/smart_ban.cpp | 4 ++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/include/libtorrent/policy.hpp b/include/libtorrent/policy.hpp index 95397b49e..204149db7 100755 --- a/include/libtorrent/policy.hpp +++ b/include/libtorrent/policy.hpp @@ -226,6 +226,8 @@ namespace libtorrent bool connect_one_peer(); bool disconnect_one_peer(); + bool has_peer(policy::peer const* p) const; + private: /* bool unchoke_one_peer(); diff --git a/src/policy.cpp b/src/policy.cpp index ec361e571..2aabffd0c 100755 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -1035,6 +1035,17 @@ namespace libtorrent p->source |= src; } + bool policy::has_peer(policy::peer const* p) const + { + // find p in m_peers + for (std::multimap::const_iterator i = m_peers.begin() + , end(m_peers.end()); i != end; ++i) + { + if (&i->second == p) return true; + } + return false; + } + policy::peer* policy::peer_from_tracker(tcp::endpoint const& remote, peer_id const& pid , int src, char flags) { diff --git a/src/smart_ban.cpp b/src/smart_ban.cpp index 550ff79ad..ab4448c1a 100644 --- a/src/smart_ban.cpp +++ b/src/smart_ban.cpp @@ -186,8 +186,8 @@ namespace libtorrent { namespace // from the first time it sent it // at least one of them must be bad - // TODO: make sure p is still a valid pointer if (p == 0) return; + if (!m_torrent.get_policy().has_peer(p)) return; #ifdef TORRENT_LOGGING char const* client = "-"; @@ -248,8 +248,8 @@ namespace libtorrent { namespace policy::peer* p = b.second.peer; - // TODO: make sure p is still a valid pointer if (p == 0) return; + if (!m_torrent.get_policy().has_peer(p)) return; #ifdef TORRENT_LOGGING char const* client = "-";