From 4d380c57d75a5b6c3c8a5dccd549fce077a1f9af Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 23 Jun 2008 22:00:27 +0000 Subject: [PATCH] made bdecode not throw --- include/libtorrent/bencode.hpp | 18 ++---------------- src/http_tracker_connection.cpp | 6 ------ src/kademlia/dht_tracker.cpp | 9 +++++++++ src/ut_metadata.cpp | 9 +++++++-- src/ut_pex.cpp | 7 ++++++- 5 files changed, 24 insertions(+), 25 deletions(-) diff --git a/include/libtorrent/bencode.hpp b/include/libtorrent/bencode.hpp index bd77aa104..9443e7a65 100755 --- a/include/libtorrent/bencode.hpp +++ b/include/libtorrent/bencode.hpp @@ -394,14 +394,7 @@ namespace libtorrent bool err = false; detail::bdecode_recursive(start, end, e, err, 0); TORRENT_ASSERT(e.m_type_queried == false); - if (err) - { -#ifdef BOOST_NO_EXCEPTIONS - return entry(); -#else - throw invalid_encoding(); -#endif - } + if (err) return entry(); return e; } @@ -414,14 +407,7 @@ namespace libtorrent detail::bdecode_recursive(start, end, e, err, 0); len = std::distance(s, start); TORRENT_ASSERT(len >= 0); - if (err) - { -#ifdef BOOST_NO_EXCEPTIONS - return entry(); -#else - throw invalid_encoding(); -#endif - } + if (err) return entry(); return e; } } diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index df0715ca6..b87ee0a28 100755 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -226,13 +226,7 @@ namespace libtorrent // handle tracker response entry e; -#ifndef BOOST_NO_EXCEPTIONS - try { -#endif e = bdecode(data, data + size); -#ifndef BOOST_NO_EXCEPTIONS - } catch (std::exception&) {} -#endif if (e.type() != entry::undefined_t) { diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index 461ea9da6..4c781b9e7 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -444,6 +444,15 @@ namespace libtorrent { namespace dht TORRENT_ASSERT(bytes_transferred > 0); entry e = bdecode(buf, buf + bytes_transferred); + if (e.type() == entry::undefined_t) + { +#ifdef TORRENT_DHT_VERBOSE_LOGGING + std::string msg(buf, buf + bytes_transferred); + TORRENT_LOG(dht_tracker) << "invalid incoming packet: " + << e.what() << "\n" << msg << "\n"; +#endif + return; + } #ifdef TORRENT_DHT_VERBOSE_LOGGING std::stringstream log_line; diff --git a/src/ut_metadata.cpp b/src/ut_metadata.cpp index f92fa88d4..0a1f139d7 100644 --- a/src/ut_metadata.cpp +++ b/src/ut_metadata.cpp @@ -306,7 +306,7 @@ namespace libtorrent { namespace if (length > 17 * 1024) { - m_pc.disconnect("ut_metadata message larger than 17 kB"); + m_pc.disconnect("ut_metadata message larger than 17 kB", 2); return true; } @@ -314,6 +314,11 @@ namespace libtorrent { namespace int len; entry msg = bdecode(body.begin, body.end, len); + if (msg.type() == entry::undefined_t) + { + m_pc.disconnect("invalid bencoding in ut_metadata message", 2); + return true; + } int type = msg["msg_type"].integer(); int piece = msg["piece"].integer(); @@ -364,7 +369,7 @@ namespace libtorrent { namespace { std::stringstream msg; msg << "unknown ut_metadata extension message: " << type; - m_pc.disconnect(msg.str().c_str()); + m_pc.disconnect(msg.str().c_str(), 2); } } return true; diff --git a/src/ut_pex.cpp b/src/ut_pex.cpp index a98fc9508..4341564f7 100644 --- a/src/ut_pex.cpp +++ b/src/ut_pex.cpp @@ -216,13 +216,18 @@ namespace libtorrent { namespace if (length > 500 * 1024) { - m_pc.disconnect("peer exchange message larger than 500 kB"); + m_pc.disconnect("peer exchange message larger than 500 kB", 2); return true; } if (body.left() < length) return true; entry pex_msg = bdecode(body.begin, body.end); + if (pex_msg.type() == entry::undefined_t) + { + m_pc.disconnect("invalid bencoding in ut_metadata message", 2); + return true; + } entry const* p = pex_msg.find_key("added"); entry const* pf = pex_msg.find_key("added.f");