fix some overflow and division by zero issues

This commit is contained in:
Arvid Norberg 2012-04-28 05:05:39 +00:00
parent 2aadba290b
commit 5583ed407a
2 changed files with 12 additions and 3 deletions

View File

@ -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;
}
}

View File

@ -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);