made bdecode not throw

This commit is contained in:
Arvid Norberg 2008-06-23 22:00:27 +00:00
parent 92f13aea68
commit 4d380c57d7
5 changed files with 24 additions and 25 deletions

View File

@ -394,14 +394,7 @@ namespace libtorrent
bool err = false; bool err = false;
detail::bdecode_recursive(start, end, e, err, 0); detail::bdecode_recursive(start, end, e, err, 0);
TORRENT_ASSERT(e.m_type_queried == false); TORRENT_ASSERT(e.m_type_queried == false);
if (err) if (err) return entry();
{
#ifdef BOOST_NO_EXCEPTIONS
return entry();
#else
throw invalid_encoding();
#endif
}
return e; return e;
} }
@ -414,14 +407,7 @@ namespace libtorrent
detail::bdecode_recursive(start, end, e, err, 0); detail::bdecode_recursive(start, end, e, err, 0);
len = std::distance(s, start); len = std::distance(s, start);
TORRENT_ASSERT(len >= 0); TORRENT_ASSERT(len >= 0);
if (err) if (err) return entry();
{
#ifdef BOOST_NO_EXCEPTIONS
return entry();
#else
throw invalid_encoding();
#endif
}
return e; return e;
} }
} }

View File

@ -226,13 +226,7 @@ namespace libtorrent
// handle tracker response // handle tracker response
entry e; entry e;
#ifndef BOOST_NO_EXCEPTIONS
try {
#endif
e = bdecode(data, data + size); e = bdecode(data, data + size);
#ifndef BOOST_NO_EXCEPTIONS
} catch (std::exception&) {}
#endif
if (e.type() != entry::undefined_t) if (e.type() != entry::undefined_t)
{ {

View File

@ -444,6 +444,15 @@ namespace libtorrent { namespace dht
TORRENT_ASSERT(bytes_transferred > 0); TORRENT_ASSERT(bytes_transferred > 0);
entry e = bdecode(buf, buf + bytes_transferred); 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 #ifdef TORRENT_DHT_VERBOSE_LOGGING
std::stringstream log_line; std::stringstream log_line;

View File

@ -306,7 +306,7 @@ namespace libtorrent { namespace
if (length > 17 * 1024) 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; return true;
} }
@ -314,6 +314,11 @@ namespace libtorrent { namespace
int len; int len;
entry msg = bdecode(body.begin, body.end, 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 type = msg["msg_type"].integer();
int piece = msg["piece"].integer(); int piece = msg["piece"].integer();
@ -364,7 +369,7 @@ namespace libtorrent { namespace
{ {
std::stringstream msg; std::stringstream msg;
msg << "unknown ut_metadata extension message: " << type; msg << "unknown ut_metadata extension message: " << type;
m_pc.disconnect(msg.str().c_str()); m_pc.disconnect(msg.str().c_str(), 2);
} }
} }
return true; return true;

View File

@ -216,13 +216,18 @@ namespace libtorrent { namespace
if (length > 500 * 1024) 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; return true;
} }
if (body.left() < length) return true; if (body.left() < length) return true;
entry pex_msg = bdecode(body.begin, body.end); 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* p = pex_msg.find_key("added");
entry const* pf = pex_msg.find_key("added.f"); entry const* pf = pex_msg.find_key("added.f");