From f81e09fe053b04b10f10daa7725cf954c5c0e6d7 Mon Sep 17 00:00:00 2001 From: Mokhtar Naamani Date: Thu, 17 Nov 2016 14:32:25 +0200 Subject: [PATCH 1/2] update stack_allocator to support 0 size allocations --- include/libtorrent/stack_allocator.hpp | 6 ++++-- test/test_stack_allocator.cpp | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/include/libtorrent/stack_allocator.hpp b/include/libtorrent/stack_allocator.hpp index f12f4cb91..2fdd4c4f3 100644 --- a/include/libtorrent/stack_allocator.hpp +++ b/include/libtorrent/stack_allocator.hpp @@ -62,6 +62,7 @@ namespace libtorrent { namespace aux int copy_buffer(char const* buf, int size) { int ret = int(m_storage.size()); + if (size < 1) return -1; m_storage.resize(ret + size); memcpy(&m_storage[ret], buf, size); return ret; @@ -69,6 +70,7 @@ namespace libtorrent { namespace aux int allocate(int bytes) { + if (bytes < 1) return -1; int ret = int(m_storage.size()); m_storage.resize(ret + bytes); return ret; @@ -76,14 +78,14 @@ namespace libtorrent { namespace aux char* ptr(int idx) { - TORRENT_ASSERT(idx >= 0); + if(idx < 0) return NULL; TORRENT_ASSERT(idx < int(m_storage.size())); return &m_storage[idx]; } char const* ptr(int idx) const { - TORRENT_ASSERT(idx >= 0); + if(idx < 0) return NULL; TORRENT_ASSERT(idx < int(m_storage.size())); return &m_storage[idx]; } diff --git a/test/test_stack_allocator.cpp b/test/test_stack_allocator.cpp index 62b1971f2..90f07f380 100644 --- a/test/test_stack_allocator.cpp +++ b/test/test_stack_allocator.cpp @@ -59,6 +59,14 @@ TORRENT_TEST(copy_buffer) a.allocate(100000); TEST_CHECK(strcmp(a.ptr(idx1), "testing") == 0); + + // attempt zero size allocation + int const idx2 = a.copy_buffer("nothing", 0); + TEST_CHECK(idx2 == -1); + + // attempt to get a pointer after zero allocation + char* ptr = a.ptr(idx2); + TEST_CHECK(ptr == NULL); } TORRENT_TEST(allocate) @@ -75,6 +83,14 @@ TORRENT_TEST(allocate) ptr = a.ptr(idx1); for (int i = 0; i < 100; ++i) TEST_CHECK(ptr[i] == char(i % 256)); + + // attempt zero size allocation + int const idx2 = a.allocate(0); + TEST_CHECK(idx2 == -1); + + // attempt to get a pointer after zero allocation + ptr = a.ptr(idx2); + TEST_CHECK(ptr == NULL); } TORRENT_TEST(swap) From c5f1c07a878485f26b64101877ac9544b2a463a6 Mon Sep 17 00:00:00 2001 From: AllSeeingEyeTolledEweSew Date: Tue, 22 Nov 2016 23:38:45 +0000 Subject: [PATCH 2/2] Bind cache_status::pieces. (#1323) expose cache_status::pieces in python binding --- bindings/python/src/session.cpp | 38 ++++++++++++++++++++++----------- bindings/python/test.py | 8 +++++++ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/bindings/python/src/session.cpp b/bindings/python/src/session.cpp index cbf78323c..91018b589 100644 --- a/bindings/python/src/session.cpp +++ b/bindings/python/src/session.cpp @@ -16,6 +16,7 @@ #include // for settings_map() #include #include // for sign_mutable_item +#include #ifndef TORRENT_NO_DEPRECATE #include @@ -465,6 +466,28 @@ namespace return ret; } + list cached_piece_info_list(std::vector const& v) + { + list pieces; + lt::time_point now = lt::clock_type::now(); + for (std::vector::const_iterator i = v.begin() + , end(v.end()); i != end; ++i) + { + dict d; + d["piece"] = i->piece; + d["last_use"] = total_milliseconds(now - i->last_use) / 1000.f; + d["next_to_hash"] = i->next_to_hash; + d["kind"] = i->kind; + pieces.append(d); + } + return pieces; + } + + list cache_status_pieces(cache_status const& cs) + { + return cached_piece_info_list(cs.pieces); + } + #ifndef TORRENT_NO_DEPRECATE cache_status get_cache_status(lt::session& s) { @@ -493,19 +516,7 @@ namespace ses.get_cache_info(ih, ret); } - list pieces; - ptime now = time_now(); - for (std::vector::iterator i = ret.begin() - , end(ret.end()); i != end; ++i) - { - dict d; - d["piece"] = i->piece; - d["last_use"] = total_milliseconds(now - i->last_use) / 1000.f; - d["next_to_hash"] = i->next_to_hash; - d["kind"] = i->kind; - pieces.append(d); - } - return pieces; + return cached_piece_info_list(ret); } #endif @@ -727,6 +738,7 @@ void bind_session() .value("flag_stop_when_ready", add_torrent_params::flag_stop_when_ready) ; class_("cache_status") + .add_property("pieces", cache_status_pieces) #ifndef TORRENT_NO_DEPRECATE .def_readonly("blocks_written", &cache_status::blocks_written) .def_readonly("writes", &cache_status::writes) diff --git a/bindings/python/test.py b/bindings/python/test.py index 9e0fa44c3..4df6bfb52 100644 --- a/bindings/python/test.py +++ b/bindings/python/test.py @@ -47,6 +47,14 @@ class test_torrent_handle(unittest.TestCase): # from python h.scrape_tracker() + def test_cache_info(self): + ses = lt.session({'alert_mask': lt.alert.category_t.all_categories, 'enable_dht': False}) + ti = lt.torrent_info('url_seed_multi.torrent'); + h = ses.add_torrent({'ti': ti, 'save_path': os.getcwd()}) + + cs = ses.get_cache_info(h) + self.assertEqual(cs.pieces, []) + class test_torrent_info(unittest.TestCase): def test_bencoded_constructor(self):