From 9f7aa7f3a6c6d95637a386bcedc4aee63b9646c2 Mon Sep 17 00:00:00 2001 From: Steven Siloti Date: Sun, 22 Nov 2015 15:56:27 -0800 Subject: [PATCH 1/2] fix segfault in put_data If less than three nodes are found to put an item to then traversal_algorithm::start will add router nodes. This leads to a crash in put_data::invoke when it tries to read a token from uninitialized memory in a null_observer. --- include/libtorrent/kademlia/put_data.hpp | 1 + src/kademlia/put_data.cpp | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/include/libtorrent/kademlia/put_data.hpp b/include/libtorrent/kademlia/put_data.hpp index 5330581b5..96a91a52d 100644 --- a/include/libtorrent/kademlia/put_data.hpp +++ b/include/libtorrent/kademlia/put_data.hpp @@ -58,6 +58,7 @@ struct put_data: traversal_algorithm put_data(node& node, put_callback const& callback); virtual char const* name() const; + virtual void start(); void set_data(item const& data) { m_data = data; } diff --git a/src/kademlia/put_data.cpp b/src/kademlia/put_data.cpp index 51e7b4556..2e7a393d8 100644 --- a/src/kademlia/put_data.cpp +++ b/src/kademlia/put_data.cpp @@ -47,6 +47,14 @@ put_data::put_data(node& dht_node, put_callback const& callback) char const* put_data::name() const { return "put_data"; } +void put_data::start() +{ + // router nodes must not be added to puts + init(); + bool is_done = add_requests(); + if (is_done) done(); +} + void put_data::set_targets(std::vector > const& targets) { for (std::vector >::const_iterator i = targets.begin() From 9c7edf803e6f69910b3aed59f3beda868a7665bf Mon Sep 17 00:00:00 2001 From: Steven Siloti Date: Sun, 22 Nov 2015 18:58:32 -0800 Subject: [PATCH 2/2] add override and a TODO --- include/libtorrent/kademlia/put_data.hpp | 2 +- src/kademlia/put_data.cpp | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/include/libtorrent/kademlia/put_data.hpp b/include/libtorrent/kademlia/put_data.hpp index 96a91a52d..a7d20d8bb 100644 --- a/include/libtorrent/kademlia/put_data.hpp +++ b/include/libtorrent/kademlia/put_data.hpp @@ -58,7 +58,7 @@ struct put_data: traversal_algorithm put_data(node& node, put_callback const& callback); virtual char const* name() const; - virtual void start(); + virtual void start() TORRENT_OVERRIDE; void set_data(item const& data) { m_data = data; } diff --git a/src/kademlia/put_data.cpp b/src/kademlia/put_data.cpp index 2e7a393d8..45bd38491 100644 --- a/src/kademlia/put_data.cpp +++ b/src/kademlia/put_data.cpp @@ -93,6 +93,9 @@ bool put_data::invoke(observer_ptr o) m_invoke_count = -1; return false; } + + // TODO: what if o is not an isntance of put_data_observer? This need to be + // redesigned for better type saftey. put_data_observer* po = static_cast(o.get()); entry e;