From 91845aa6c804a8e7a8556bf6d509d7ca76b0e5cf Mon Sep 17 00:00:00 2001 From: Pavel Pimenov Date: Thu, 20 Oct 2016 00:28:35 +0300 Subject: [PATCH] Prevent duplicate url (#1231) prevent creating torrents with duplicate trackers as well as adding duplicate trackers to torrent_info --- src/create_torrent.cpp | 3 +++ src/torrent_info.cpp | 5 +++++ test/test_create_torrent.cpp | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index 1f94dbc85..0e306bd47 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -664,6 +664,9 @@ namespace libtorrent void create_torrent::add_tracker(string_view url, int const tier) { + auto i = std::find_if(m_urls.begin(), m_urls.end() + , [&url](announce_entry const& ae) { return ae.first == url.to_string(); }); + if (i != m_urls.end()) return; m_urls.push_back(announce_entry(url.to_string(), tier)); std::sort(m_urls.begin(), m_urls.end() diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 5bbcba528..9c9665e1b 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -1347,6 +1347,7 @@ namespace libtorrent if (ec) return false; m_info_hash = p.info_hash; + m_urls.reserve(m_urls.size() + p.trackers.size()); for (auto const& url : p.trackers) m_urls.push_back(announce_entry(url)); @@ -1534,6 +1535,10 @@ namespace libtorrent void torrent_info::add_tracker(std::string const& url, int tier) { + auto i = std::find_if(m_urls.begin(), m_urls.end() + , [&url](announce_entry const& ae) { return ae.url == url; }); + if (i != m_urls.end()) return; + announce_entry e(url); e.tier = tier; e.source = announce_entry::source_client; diff --git a/test/test_create_torrent.cpp b/test/test_create_torrent.cpp index 7fcf33c57..7423d5bb4 100644 --- a/test/test_create_torrent.cpp +++ b/test/test_create_torrent.cpp @@ -36,6 +36,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/create_torrent.hpp" #include "libtorrent/bencode.hpp" #include "libtorrent/aux_/escape_string.hpp" // for convert_path_to_posix +#include "libtorrent/announce_entry.hpp" #include @@ -49,6 +50,10 @@ TORRENT_TEST(create_verbatim_torrent) lt::torrent_info info(test_torrent, sizeof(test_torrent) - 1); + info.add_tracker("http://test.com"); + info.add_tracker("http://test.com"); + TEST_EQUAL(info.trackers().size(), 1); + lt::create_torrent t(info); std::vector buffer;