From 0d27a0acdd0cf0997bb9d698ae431d290c93beba Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 14 May 2016 13:24:29 -0400 Subject: [PATCH] fix assert when adding an empty file (as a torrent) (#729) --- src/session_impl.cpp | 6 ++++++ test/test_session.cpp | 21 +++++++++++++++++---- test/test_torrent_info.cpp | 26 +++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 7 deletions(-) diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 456def482..ede02846f 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -4818,6 +4818,12 @@ namespace aux { params.ti = t; } + if (params.ti && !params.ti->is_valid()) + { + ec = errors::no_metadata; + return ptr_t(); + } + if (params.ti && params.ti->is_valid() && params.ti->num_files() == 0) { ec = errors::no_files_in_torrent; diff --git a/test/test_session.cpp b/test/test_session.cpp index d7585753a..c6ea728a8 100644 --- a/test/test_session.cpp +++ b/test/test_session.cpp @@ -40,6 +40,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/performance_counters.hpp" #include "libtorrent/bdecode.hpp" #include "libtorrent/bencode.hpp" +#include "libtorrent/torrent_info.hpp" using namespace libtorrent; namespace lt = libtorrent; @@ -143,6 +144,22 @@ TORRENT_TEST(async_add_torrent_duplicate) TEST_CHECK(!a->error); } +TORRENT_TEST(load_empty_file) +{ + settings_pack p; + p.set_int(settings_pack::alert_mask, ~0); + lt::session ses(p); + + add_torrent_params atp; + error_code ignore_errors; + atp.ti = boost::make_shared("", 0, ignore_errors); + atp.save_path = "."; + error_code ec; + torrent_handle h = ses.add_torrent(atp, ec); + + TEST_CHECK(!h.is_valid()); + TEST_CHECK(ec == error_code(errors::no_metadata)) +} TORRENT_TEST(session_stats) { @@ -158,8 +175,6 @@ TORRENT_TEST(session_stats) TEST_EQUAL(stats[i].value_index, i); } } -#if __cplusplus >= 201103L - template void test_save_restore(Set setup, Save s, Default d, Load l) { @@ -260,5 +275,3 @@ TORRENT_TEST(save_restore_state_load_filter) }); } -#endif - diff --git a/test/test_torrent_info.cpp b/test/test_torrent_info.cpp index 3bf25fffe..990cbf549 100644 --- a/test/test_torrent_info.cpp +++ b/test/test_torrent_info.cpp @@ -612,7 +612,7 @@ TORRENT_TEST(parse_torrents) fprintf(stderr, "loading %s\n", test_torrents[i].file); std::string filename = combine_path(combine_path(root_dir, "test_torrents") , test_torrents[i].file); - boost::shared_ptr ti(new torrent_info(filename, ec)); + auto ti = boost::make_shared(filename, ec); TEST_CHECK(!ec); if (ec) fprintf(stderr, " loading(\"%s\") -> failed %s\n", filename.c_str() , ec.message().c_str()); @@ -751,8 +751,8 @@ TORRENT_TEST(parse_torrents) { error_code ec; fprintf(stderr, "loading %s\n", test_error_torrents[i].file); - boost::shared_ptr ti(new torrent_info(combine_path( - combine_path(root_dir, "test_torrents"), test_error_torrents[i].file), ec)); + auto ti = boost::make_shared(combine_path( + combine_path(root_dir, "test_torrents"), test_error_torrents[i].file), ec); fprintf(stderr, "E: \"%s\"\nexpected: \"%s\"\n", ec.message().c_str() , test_error_torrents[i].error.message().c_str()); TEST_CHECK(ec.message() == test_error_torrents[i].error.message()); @@ -854,6 +854,26 @@ TORRENT_TEST(resolve_duplicates) test_resolve_duplicates(i); } +TORRENT_TEST(empty_file) +{ + error_code ec; + auto ti = boost::make_shared("", 0, ec); + TEST_CHECK(ec); +} + +TORRENT_TEST(empty_file2) +{ + try + { + auto ti = boost::make_shared("", 0); + TEST_ERROR("expected exception thrown"); + } + catch (libtorrent_exception& e) + { + printf("Expected error: %s\n", e.error().message().c_str()); + } +} + TORRENT_TEST(copy) { using namespace libtorrent;