From 5583ed407aa6cb36e348b15ce300496bfe4d0c53 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 28 Apr 2012 05:05:39 +0000 Subject: [PATCH] fix some overflow and division by zero issues --- src/bandwidth_queue_entry.cpp | 5 ++++- src/peer_connection.cpp | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/bandwidth_queue_entry.cpp b/src/bandwidth_queue_entry.cpp index c13a564a9..6833b88d3 100644 --- a/src/bandwidth_queue_entry.cpp +++ b/src/bandwidth_queue_entry.cpp @@ -54,9 +54,13 @@ namespace libtorrent TORRENT_ASSERT(assigned < request_size); int quota = request_size - assigned; TORRENT_ASSERT(quota >= 0); + --ttl; + if (quota == 0) return quota; + for (int j = 0; j < 5 && channel[j]; ++j) { if (channel[j]->throttle() == 0) continue; + if (channel[j]->tmp == 0) continue; quota = (std::min)(int(boost::int64_t(channel[j]->distribute_quota) * priority / channel[j]->tmp), quota); } @@ -64,7 +68,6 @@ namespace libtorrent for (int j = 0; j < 5 && channel[j]; ++j) channel[j]->use_quota(quota); TORRENT_ASSERT(assigned <= request_size); - --ttl; return quota; } } diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 169676044..133ada424 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -4702,7 +4702,13 @@ namespace libtorrent // assume 20 kB/s upload_capacity = (std::max)(20000, m_ses.m_peak_up_rate + 10000); } - priority = (boost::uint64_t(m_est_reciprocation_rate) << 10) / upload_capacity; + int estimated_reciprocation_rate = m_est_reciprocation_rate; + // we cannot send faster than our upload rate anyway + if (estimated_reciprocation_rate < upload_capacity) + estimated_reciprocation_rate = upload_capacity; + + priority = (boost::uint64_t(estimated_reciprocation_rate) << 14) / upload_capacity; + if (priority > 0xffff) priority = 0xffff; } else { @@ -4710,7 +4716,7 @@ namespace libtorrent if (priority > 255) priority = 255; priority += t->priority() << 8; } - TORRENT_ASSERT(priority < 0xffff); + TORRENT_ASSERT(priority <= 0xffff); // peers that we are not interested in are non-prioritized TORRENT_ASSERT((m_channel_state[upload_channel] & peer_info::bw_limit) == 0);