fixing sign-conversion warnings, part 13, minor refactor (#1675)

fixing sign-conversion warnings, part 13, minor refactor
This commit is contained in:
Alden Torres 2017-02-07 23:36:13 -05:00 committed by Arvid Norberg
parent c8569b5e34
commit 1f72843fe3
9 changed files with 87 additions and 80 deletions

View File

@ -47,6 +47,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/assert.hpp" #include "libtorrent/assert.hpp"
#include "libtorrent/span.hpp" #include "libtorrent/span.hpp"
#include "libtorrent/buffer.hpp" #include "libtorrent/buffer.hpp"
#include "libtorrent/aux_/array.hpp"
#include <list> #include <list>
#include <array> #include <array>
@ -63,7 +64,7 @@ namespace libtorrent
// RC4 state from libtomcrypt // RC4 state from libtomcrypt
struct rc4 { struct rc4 {
int x, y; int x, y;
std::array<std::uint8_t, 256> buf; aux::array<std::uint8_t, 256> buf;
}; };
// TODO: 3 dh_key_exchange should probably move into its own file // TODO: 3 dh_key_exchange should probably move into its own file

View File

@ -934,7 +934,7 @@ namespace libtorrent
// it just serves as a queue to remember what we've sent, to avoid // it just serves as a queue to remember what we've sent, to avoid
// re-sending suggests for the same piece // re-sending suggests for the same piece
// i.e. outgoing suggest pieces // i.e. outgoing suggest pieces
std::vector<piece_index_t> m_suggest_pieces; aux::vector<piece_index_t> m_suggest_pieces;
// the pieces we will send to the peer // the pieces we will send to the peer
// if requested (regardless of choke state) // if requested (regardless of choke state)
@ -952,7 +952,7 @@ namespace libtorrent
// pieces that has been suggested to be downloaded from this peer // pieces that has been suggested to be downloaded from this peer
// i.e. incoming suggestions // i.e. incoming suggestions
// TODO: 2 this should really be a circular buffer // TODO: 2 this should really be a circular buffer
std::vector<piece_index_t> m_suggested_pieces; aux::vector<piece_index_t> m_suggested_pieces;
// the time when this peer last saw a complete copy // the time when this peer last saw a complete copy
// of this torrent // of this torrent

View File

@ -246,7 +246,8 @@ namespace libtorrent
std::tie(payload, protocol) = m_parser.incoming( std::tie(payload, protocol) = m_parser.incoming(
recv_buffer, parse_error); recv_buffer, parse_error);
received_bytes(0, protocol); received_bytes(0, protocol);
bytes_transferred -= protocol; TORRENT_ASSERT(bytes_transferred >= aux::numeric_cast<std::size_t>(protocol));
bytes_transferred -= aux::numeric_cast<std::size_t>(protocol);
#if TORRENT_USE_ASSERTS #if TORRENT_USE_ASSERTS
if (payload > front_request.length) payload = front_request.length; if (payload > front_request.length) payload = front_request.length;
#endif #endif
@ -348,7 +349,7 @@ namespace libtorrent
m_body_start = m_parser.body_start(); m_body_start = m_parser.body_start();
} }
recv_buffer = recv_buffer.subspan(m_body_start); recv_buffer = recv_buffer.subspan(aux::numeric_cast<std::size_t>(m_body_start));
// ========================= // =========================
// === CHUNKED ENCODING === // === CHUNKED ENCODING ===
@ -359,15 +360,15 @@ namespace libtorrent
{ {
int header_size = 0; int header_size = 0;
std::int64_t chunk_size = 0; std::int64_t chunk_size = 0;
span<char const> chunk_start(recv_buffer.begin() + m_chunk_pos, std::size_t(int(recv_buffer.size()) - m_chunk_pos)); span<char const> chunk_start(recv_buffer.begin() + m_chunk_pos, aux::numeric_cast<std::size_t>(int(recv_buffer.size()) - m_chunk_pos));
TORRENT_ASSERT(chunk_start[0] == '\r' TORRENT_ASSERT(chunk_start[0] == '\r'
|| aux::is_hex(chunk_start[0])); || aux::is_hex(chunk_start[0]));
bool ret = m_parser.parse_chunk_header(chunk_start, &chunk_size, &header_size); bool ret = m_parser.parse_chunk_header(chunk_start, &chunk_size, &header_size);
if (!ret) if (!ret)
{ {
TORRENT_ASSERT(bytes_transferred >= size_t(chunk_start.size() - m_partial_chunk_header)); TORRENT_ASSERT(bytes_transferred >= aux::numeric_cast<std::size_t>(int(chunk_start.size()) - m_partial_chunk_header));
bytes_transferred -= chunk_start.size() - m_partial_chunk_header; bytes_transferred -= aux::numeric_cast<std::size_t>(int(chunk_start.size()) - m_partial_chunk_header);
received_bytes(0, int(chunk_start.size() - m_partial_chunk_header)); received_bytes(0, int(chunk_start.size()) - m_partial_chunk_header);
m_partial_chunk_header = int(chunk_start.size()); m_partial_chunk_header = int(chunk_start.size());
if (bytes_transferred == 0) return; if (bytes_transferred == 0) return;
break; break;
@ -379,23 +380,23 @@ namespace libtorrent
, "parsed chunk: %" PRId64 " header_size: %d" , "parsed chunk: %" PRId64 " header_size: %d"
, chunk_size, header_size); , chunk_size, header_size);
#endif #endif
TORRENT_ASSERT(bytes_transferred >= size_t(header_size - m_partial_chunk_header)); TORRENT_ASSERT(bytes_transferred >= aux::numeric_cast<std::size_t>(header_size - m_partial_chunk_header));
bytes_transferred -= header_size - m_partial_chunk_header; bytes_transferred -= aux::numeric_cast<std::size_t>(header_size - m_partial_chunk_header);
received_bytes(0, header_size - m_partial_chunk_header); received_bytes(0, header_size - m_partial_chunk_header);
m_partial_chunk_header = 0; m_partial_chunk_header = 0;
TORRENT_ASSERT(chunk_size != 0 || int(chunk_start.size()) <= header_size || chunk_start[header_size] == 'H'); TORRENT_ASSERT(chunk_size != 0 || int(chunk_start.size()) <= header_size || chunk_start[std::size_t(header_size)] == 'H');
// cut out the chunk header from the receive buffer // cut out the chunk header from the receive buffer
TORRENT_ASSERT(m_chunk_pos + m_body_start < INT_MAX); TORRENT_ASSERT(m_chunk_pos + m_body_start < INT_MAX);
m_recv_buffer.cut(header_size, t->block_size() + 1024, int(m_chunk_pos + m_body_start)); m_recv_buffer.cut(header_size, t->block_size() + 1024, int(m_chunk_pos + m_body_start));
recv_buffer = m_recv_buffer.get(); recv_buffer = m_recv_buffer.get();
recv_buffer = recv_buffer.subspan(m_body_start); recv_buffer = recv_buffer.subspan(aux::numeric_cast<std::size_t>(m_body_start));
m_chunk_pos += chunk_size; m_chunk_pos += chunk_size;
if (chunk_size == 0) if (chunk_size == 0)
{ {
TORRENT_ASSERT(int(m_recv_buffer.get().size()) < m_chunk_pos + m_body_start + 1 TORRENT_ASSERT(int(m_recv_buffer.get().size()) < m_chunk_pos + m_body_start + 1
|| m_recv_buffer.get()[int(m_chunk_pos + m_body_start)] == 'H' || m_recv_buffer.get()[aux::numeric_cast<std::size_t>(m_chunk_pos + m_body_start)] == 'H'
|| (m_parser.chunked_encoding() && m_recv_buffer.get()[int(m_chunk_pos + m_body_start)] == '\r')); || (m_parser.chunked_encoding() && m_recv_buffer.get()[aux::numeric_cast<std::size_t>(m_chunk_pos + m_body_start)] == '\r'));
m_chunk_pos = -1; m_chunk_pos = -1;
} }
} }
@ -441,13 +442,14 @@ namespace libtorrent
int size_to_cut = m_body_start + front_request.length; int size_to_cut = m_body_start + front_request.length;
TORRENT_ASSERT(int(m_recv_buffer.get().size()) < size_to_cut + 1 TORRENT_ASSERT(int(m_recv_buffer.get().size()) < size_to_cut + 1
|| m_recv_buffer.get()[size_to_cut] == 'H' || m_recv_buffer.get()[aux::numeric_cast<std::size_t>(size_to_cut)] == 'H'
|| (m_parser.chunked_encoding() && m_recv_buffer.get()[size_to_cut] == '\r')); || (m_parser.chunked_encoding() && m_recv_buffer.get()[aux::numeric_cast<std::size_t>(size_to_cut)] == '\r'));
m_recv_buffer.cut(size_to_cut, t->block_size() + 1024); m_recv_buffer.cut(size_to_cut, t->block_size() + 1024);
if (m_response_left == 0) m_chunk_pos = 0; if (m_response_left == 0) m_chunk_pos = 0;
else m_chunk_pos -= front_request.length; else m_chunk_pos -= front_request.length;
bytes_transferred -= payload; TORRENT_ASSERT(bytes_transferred >= aux::numeric_cast<std::size_t>(payload));
bytes_transferred -= aux::numeric_cast<std::size_t>(payload);
m_body_start = 0; m_body_start = 0;
if (m_response_left > 0) continue; if (m_response_left > 0) continue;
TORRENT_ASSERT(m_response_left == 0); TORRENT_ASSERT(m_response_left == 0);

View File

@ -256,17 +256,17 @@ namespace libtorrent
if (m_data.dict == nullptr) if (m_data.dict == nullptr)
{ {
int capacity = lazy_entry_dict_init; int capacity = lazy_entry_dict_init;
m_data.dict = new (std::nothrow) lazy_dict_entry[capacity+1]; m_data.dict = new (std::nothrow) lazy_dict_entry[capacity + 1];
if (m_data.dict == nullptr) return nullptr; if (m_data.dict == nullptr) return nullptr;
m_data.dict[0].val.m_len = capacity; m_data.dict[0].val.m_len = capacity;
} }
else if (int(m_size) == this->capacity()) else if (int(m_size) == this->capacity())
{ {
int capacity = this->capacity() * lazy_entry_grow_factor / 100; int capacity = this->capacity() * lazy_entry_grow_factor / 100;
lazy_dict_entry* tmp = new (std::nothrow) lazy_dict_entry[capacity+1]; lazy_dict_entry* tmp = new (std::nothrow) lazy_dict_entry[capacity + 1];
if (tmp == nullptr) return nullptr; if (tmp == nullptr) return nullptr;
std::memcpy(tmp, m_data.dict, sizeof(lazy_dict_entry) * (m_size + 1)); std::memcpy(tmp, m_data.dict, sizeof(lazy_dict_entry) * (m_size + 1));
for (int i = 0; i < int(m_size); ++i) m_data.dict[i+1].val.release(); for (int i = 0; i < int(m_size); ++i) m_data.dict[i + 1].val.release();
delete[] m_data.dict; delete[] m_data.dict;
m_data.dict = tmp; m_data.dict = tmp;
@ -274,7 +274,7 @@ namespace libtorrent
} }
TORRENT_ASSERT(int(m_size) < this->capacity()); TORRENT_ASSERT(int(m_size) < this->capacity());
lazy_dict_entry& ret = m_data.dict[1+m_size++]; lazy_dict_entry& ret = m_data.dict[1 + m_size++];
ret.name = name; ret.name = name;
return &ret.val; return &ret.val;
} }
@ -300,7 +300,7 @@ namespace libtorrent
} }
} }
void lazy_entry::construct_string(char const* start, int length) void lazy_entry::construct_string(char const* start, int const length)
{ {
TORRENT_ASSERT(m_type == none_t); TORRENT_ASSERT(m_type == none_t);
m_type = string_t; m_type = string_t;
@ -328,11 +328,11 @@ namespace libtorrent
} }
} }
std::pair<std::string, lazy_entry const*> lazy_entry::dict_at(int i) const std::pair<std::string, lazy_entry const*> lazy_entry::dict_at(int const i) const
{ {
TORRENT_ASSERT(m_type == dict_t); TORRENT_ASSERT(m_type == dict_t);
TORRENT_ASSERT(i < int(m_size)); TORRENT_ASSERT(i < int(m_size));
lazy_dict_entry const& e = m_data.dict[i+1]; lazy_dict_entry const& e = m_data.dict[i + 1];
return std::make_pair(std::string(e.name, e.val.m_begin - e.name), &e.val); return std::make_pair(std::string(e.name, e.val.m_begin - e.name), &e.val);
} }
@ -425,17 +425,17 @@ namespace libtorrent
if (m_data.start == nullptr) if (m_data.start == nullptr)
{ {
int capacity = lazy_entry_list_init; int capacity = lazy_entry_list_init;
m_data.list = new (std::nothrow) lazy_entry[capacity+1]; m_data.list = new (std::nothrow) lazy_entry[capacity + 1];
if (m_data.list == nullptr) return nullptr; if (m_data.list == nullptr) return nullptr;
m_data.list[0].m_len = capacity; m_data.list[0].m_len = capacity;
} }
else if (int(m_size) == this->capacity()) else if (int(m_size) == this->capacity())
{ {
int capacity = this->capacity() * lazy_entry_grow_factor / 100; int capacity = this->capacity() * lazy_entry_grow_factor / 100;
lazy_entry* tmp = new (std::nothrow) lazy_entry[capacity+1]; lazy_entry* tmp = new (std::nothrow) lazy_entry[capacity + 1];
if (tmp == nullptr) return nullptr; if (tmp == nullptr) return nullptr;
std::memcpy(tmp, m_data.list, sizeof(lazy_entry) * (m_size+1)); std::memcpy(tmp, m_data.list, sizeof(lazy_entry) * (m_size + 1));
for (int i = 0; i < int(m_size); ++i) m_data.list[i+1].release(); for (int i = 0; i < int(m_size); ++i) m_data.list[i + 1].release();
delete[] m_data.list; delete[] m_data.list;
m_data.list = tmp; m_data.list = tmp;
@ -559,7 +559,7 @@ namespace libtorrent
} }
} }
void print_string(std::string& ret, char const* str, int len, bool single_line) void print_string(std::string& ret, char const* str, int const len, bool single_line)
{ {
bool printable = true; bool printable = true;
for (int i = 0; i < len; ++i) for (int i = 0; i < len; ++i)
@ -576,7 +576,7 @@ namespace libtorrent
{ {
ret.append(str, 14); ret.append(str, 14);
ret += "..."; ret += "...";
ret.append(str + len-14, 14); ret.append(str + len - 14, 14);
} }
else else
ret.append(str, len); ret.append(str, len);
@ -604,7 +604,7 @@ namespace libtorrent
indent_str[0] = ','; indent_str[0] = ',';
indent_str[1] = '\n'; indent_str[1] = '\n';
indent_str[199] = 0; indent_str[199] = 0;
if (indent < 197 && indent >= 0) indent_str[indent+2] = 0; if (indent < 197 && indent >= 0) indent_str[indent + 2] = 0;
std::string ret; std::string ret;
switch (e.type()) switch (e.type())
{ {

View File

@ -75,8 +75,8 @@ namespace libtorrent
if (end < begin + 96) if (end < begin + 96)
{ {
int const len = int(end - begin); int const len = int(end - begin);
std::memmove(begin + 96 - len, begin, len); std::memmove(begin + 96 - len, begin, aux::numeric_cast<std::size_t>(len));
std::memset(begin, 0, 96 - len); std::memset(begin, 0, aux::numeric_cast<std::size_t>(96 - len));
} }
return ret; return ret;
} }
@ -143,7 +143,7 @@ namespace libtorrent
if (to_process < size) if (to_process < size)
{ {
new (&bufs[i]) span<char>( new (&bufs[i]) span<char>(
iovec[i].data(), to_process); iovec[i].data(), aux::numeric_cast<std::size_t>(to_process));
to_process = 0; to_process = 0;
} }
else else
@ -222,7 +222,8 @@ namespace libtorrent
std::tie(consume, produce, packet_size) = m_dec_handler->decrypt(wr_buf); std::tie(consume, produce, packet_size) = m_dec_handler->decrypt(wr_buf);
TORRENT_ASSERT(packet_size || produce); TORRENT_ASSERT(packet_size || produce);
TORRENT_ASSERT(packet_size >= 0); TORRENT_ASSERT(packet_size >= 0);
bytes_transferred = produce; TORRENT_ASSERT(produce >= 0);
bytes_transferred = std::size_t(produce);
if (packet_size) if (packet_size)
recv_buffer.crypto_cut(consume, packet_size); recv_buffer.crypto_cut(consume, packet_size);
} }
@ -320,7 +321,7 @@ namespace libtorrent
TORRENT_ASSERT(pos); TORRENT_ASSERT(pos);
bytes_processed += len; bytes_processed += len;
rc4_encrypt(pos, len, &m_rc4_outgoing); rc4_encrypt(pos, std::uint32_t(len), &m_rc4_outgoing);
} }
return std::make_tuple(bytes_processed, empty); return std::make_tuple(bytes_processed, empty);
} }
@ -339,7 +340,7 @@ namespace libtorrent
TORRENT_ASSERT(pos); TORRENT_ASSERT(pos);
bytes_processed += len; bytes_processed += len;
rc4_encrypt(pos, len, &m_rc4_incoming); rc4_encrypt(pos, std::uint32_t(len), &m_rc4_incoming);
} }
return std::make_tuple(0, bytes_processed, 0); return std::make_tuple(0, bytes_processed, 0);
} }
@ -351,7 +352,7 @@ namespace libtorrent
void rc4_init(const unsigned char* in, unsigned long len, rc4 *state) void rc4_init(const unsigned char* in, unsigned long len, rc4 *state)
{ {
size_t const key_size = sizeof(state->buf); size_t const key_size = sizeof(state->buf);
std::array<std::uint8_t, key_size> key; aux::array<std::uint8_t, key_size> key;
std::uint8_t tmp, *s; std::uint8_t tmp, *s;
int keylen, x, y, j; int keylen, x, y, j;
@ -371,7 +372,7 @@ void rc4_init(const unsigned char* in, unsigned long len, rc4 *state)
/* make RC4 perm and shuffle */ /* make RC4 perm and shuffle */
for (x = 0; x < int(key_size); ++x) { for (x = 0; x < int(key_size); ++x) {
s[x] = std::uint8_t(x); s[x] = x & 0xff;
} }
for (j = x = y = 0; x < int(key_size); x++) { for (j = x = y = 0; x < int(key_size); x++) {

View File

@ -1550,14 +1550,14 @@ namespace libtorrent
// the later the suggestion is received, the higher priority we should // the later the suggestion is received, the higher priority we should
// ascribe to it, so we need to insert suggestions at the front of the // ascribe to it, so we need to insert suggestions at the front of the
// queue. // queue.
if (int(m_suggested_pieces.size()) > m_settings.get_int(settings_pack::max_suggest_pieces)) if (m_suggested_pieces.end_index() > m_settings.get_int(settings_pack::max_suggest_pieces))
m_suggested_pieces.resize(m_settings.get_int(settings_pack::max_suggest_pieces) - 1); m_suggested_pieces.resize(m_settings.get_int(settings_pack::max_suggest_pieces) - 1);
m_suggested_pieces.insert(m_suggested_pieces.begin(), index); m_suggested_pieces.insert(m_suggested_pieces.begin(), index);
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::info, "SUGGEST_PIECE", "piece: %d added to set: %d" peer_log(peer_log_alert::info, "SUGGEST_PIECE", "piece: %d added to set: %d"
, static_cast<int>(index), int(m_suggested_pieces.size())); , static_cast<int>(index), m_suggested_pieces.end_index());
#endif #endif
} }
@ -2008,9 +2008,9 @@ namespace libtorrent
if (should_log(peer_log_alert::incoming_message)) if (should_log(peer_log_alert::incoming_message))
{ {
std::string bitfield_str; std::string bitfield_str;
bitfield_str.resize(bits.size()); bitfield_str.resize(aux::numeric_cast<std::size_t>(bits.size()));
for (piece_index_t i(0); i != bits.end_index(); ++i) for (piece_index_t i(0); i != bits.end_index(); ++i)
bitfield_str[static_cast<int>(i)] = bits[i] ? '1' : '0'; bitfield_str[std::size_t(static_cast<int>(i))] = bits[i] ? '1' : '0';
peer_log(peer_log_alert::incoming_message, "BITFIELD" peer_log(peer_log_alert::incoming_message, "BITFIELD"
, "%s", bitfield_str.c_str()); , "%s", bitfield_str.c_str());
} }
@ -3785,9 +3785,9 @@ namespace libtorrent
send_suggest(*i); send_suggest(*i);
} }
int const max = m_settings.get_int(settings_pack::max_suggest_pieces); int const max = m_settings.get_int(settings_pack::max_suggest_pieces);
if (int(m_suggest_pieces.size()) > max) if (m_suggest_pieces.end_index() > max)
{ {
int const to_erase = int(m_suggest_pieces.size() - max); int const to_erase = m_suggest_pieces.end_index() - max;
m_suggest_pieces.erase(m_suggest_pieces.begin() m_suggest_pieces.erase(m_suggest_pieces.begin()
, m_suggest_pieces.begin() + to_erase); , m_suggest_pieces.begin() + to_erase);
} }
@ -3878,7 +3878,7 @@ namespace libtorrent
m_counters.inc_stats_counter(counters::num_peers_down_requests); m_counters.inc_stats_counter(counters::num_peers_down_requests);
TORRENT_ASSERT(verify_piece(t->to_req(block.block))); TORRENT_ASSERT(verify_piece(t->to_req(block.block)));
block.send_buffer_offset = m_send_buffer.size(); block.send_buffer_offset = aux::numeric_cast<std::uint32_t>(m_send_buffer.size());
m_download_queue.push_back(block); m_download_queue.push_back(block);
m_outstanding_bytes += block_size; m_outstanding_bytes += block_size;
#if TORRENT_USE_INVARIANT_CHECKS #if TORRENT_USE_INVARIANT_CHECKS
@ -3906,7 +3906,7 @@ namespace libtorrent
if (m_download_queue.empty()) if (m_download_queue.empty())
m_counters.inc_stats_counter(counters::num_peers_down_requests); m_counters.inc_stats_counter(counters::num_peers_down_requests);
block.send_buffer_offset = m_send_buffer.size(); block.send_buffer_offset = aux::numeric_cast<std::uint32_t>(m_send_buffer.size());
m_download_queue.push_back(block); m_download_queue.push_back(block);
if (m_queued_time_critical) --m_queued_time_critical; if (m_queued_time_critical) --m_queued_time_critical;
@ -4482,7 +4482,7 @@ namespace libtorrent
#else #else
p.progress = float(p.pieces.count()) / float(p.pieces.size()); p.progress = float(p.pieces.count()) / float(p.pieces.size());
#endif #endif
p.progress_ppm = int(std::uint64_t(p.pieces.count()) * 1000000 / p.pieces.size()); p.progress_ppm = int(std::int64_t(p.pieces.count()) * 1000000 / p.pieces.size());
} }
p.estimated_reciprocation_rate = m_est_reciprocation_rate; p.estimated_reciprocation_rate = m_est_reciprocation_rate;
@ -5677,7 +5677,7 @@ namespace libtorrent
int const alloc_buf_size = m_ses.send_buffer_size(); int const alloc_buf_size = m_ses.send_buffer_size();
int const buf_size = std::min(alloc_buf_size, size); int const buf_size = std::min(alloc_buf_size, size);
std::memcpy(session_buf.get(), buf, buf_size); std::memcpy(session_buf.get(), buf, aux::numeric_cast<std::size_t>(buf_size));
buf += buf_size; buf += buf_size;
size -= buf_size; size -= buf_size;
m_send_buffer.append_buffer(std::move(session_buf), alloc_buf_size, buf_size); m_send_buffer.append_buffer(std::move(session_buf), alloc_buf_size, buf_size);
@ -5846,9 +5846,9 @@ namespace libtorrent
int sub_transferred = 0; int sub_transferred = 0;
do { do {
sub_transferred = m_recv_buffer.advance_pos(bytes); sub_transferred = m_recv_buffer.advance_pos(bytes);
on_receive(error, sub_transferred);
bytes -= sub_transferred;
TORRENT_ASSERT(sub_transferred > 0); TORRENT_ASSERT(sub_transferred > 0);
on_receive(error, std::size_t(sub_transferred));
bytes -= sub_transferred;
if (m_disconnecting) return; if (m_disconnecting) return;
} while (bytes > 0 && sub_transferred > 0); } while (bytes > 0 && sub_transferred > 0);

View File

@ -44,22 +44,24 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent { namespace aux namespace libtorrent { namespace aux
{ {
int copy_bufs(span<iovec_t const> bufs, int bytes, span<iovec_t> target) int copy_bufs(span<iovec_t const> bufs, int const bytes, span<iovec_t> target)
{ {
int size = 0; int size = 0;
for (int i = 0;; i++) for (int i = 0;; i++)
{ {
target[i] = bufs[i]; std::size_t const idx = std::size_t(i);
size += int(bufs[i].iov_len); target[idx] = bufs[idx];
size += int(bufs[idx].iov_len);
if (size >= bytes) if (size >= bytes)
{ {
target[i].iov_len -= size - bytes; TORRENT_ASSERT(target[idx].iov_len >= aux::numeric_cast<std::size_t>(size - bytes));
target[idx].iov_len -= aux::numeric_cast<std::size_t>(size - bytes);
return i + 1; return i + 1;
} }
} }
} }
span<iovec_t> advance_bufs(span<iovec_t> bufs, int bytes) span<iovec_t> advance_bufs(span<iovec_t> bufs, int const bytes)
{ {
int size = 0; int size = 0;
for (;;) for (;;)
@ -69,7 +71,7 @@ namespace libtorrent { namespace aux
{ {
bufs.front().iov_base = reinterpret_cast<char*>(bufs.front().iov_base) bufs.front().iov_base = reinterpret_cast<char*>(bufs.front().iov_base)
+ bufs.front().iov_len - (size - bytes); + bufs.front().iov_len - (size - bytes);
bufs.front().iov_len = size - bytes; bufs.front().iov_len = aux::numeric_cast<std::size_t>(size - bytes);
return bufs; return bufs;
} }
bufs = bufs.subspan(1); bufs = bufs.subspan(1);

View File

@ -611,7 +611,7 @@ namespace libtorrent
// forces the torrent to stay loaded while the client holds it // forces the torrent to stay loaded while the client holds it
torrent_info const& torrent_handle::get_torrent_info() const torrent_info const& torrent_handle::get_torrent_info() const
{ {
static std::shared_ptr<const torrent_info> holder[4]; static aux::array<std::shared_ptr<const torrent_info>, 4> holder;
static int cursor = 0; static int cursor = 0;
static std::mutex holder_mutex; static std::mutex holder_mutex;
@ -619,7 +619,7 @@ namespace libtorrent
std::lock_guard<std::mutex> l(holder_mutex); std::lock_guard<std::mutex> l(holder_mutex);
holder[cursor++] = r; holder[cursor++] = r;
cursor = cursor % (sizeof(holder) / sizeof(holder[0])); cursor = cursor % holder.end_index();;
return *r; return *r;
} }

View File

@ -51,6 +51,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/magnet_uri.hpp" #include "libtorrent/magnet_uri.hpp"
#include "libtorrent/announce_entry.hpp" #include "libtorrent/announce_entry.hpp"
#include "libtorrent/hex.hpp" // to_hex #include "libtorrent/hex.hpp" // to_hex
#include "libtorrent/aux_/numeric_cast.hpp"
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
#include "libtorrent/lazy_entry.hpp" #include "libtorrent/lazy_entry.hpp"
@ -238,7 +239,7 @@ namespace libtorrent
{ {
// 2 bytes // 2 bytes
if (element.size() - i < 2 if (element.size() - i < 2
|| (element[i+1] & 0xc0) != 0x80) || (element[i + 1] & 0xc0) != 0x80)
{ {
path += '_'; path += '_';
last_len = 1; last_len = 1;
@ -261,8 +262,8 @@ namespace libtorrent
{ {
// 3 bytes // 3 bytes
if (element.size() - i < 3 if (element.size() - i < 3
|| (element[i+1] & 0xc0) != 0x80 || (element[i + 1] & 0xc0) != 0x80
|| (element[i+2] & 0xc0) != 0x80 || (element[i + 2] & 0xc0) != 0x80
) )
{ {
path += '_'; path += '_';
@ -335,7 +336,7 @@ namespace libtorrent
for (int j = int(element.size()) - 1; for (int j = int(element.size()) - 1;
j > std::max(int(element.size()) - 10, int(i)); --j) j > std::max(int(element.size()) - 10, int(i)); --j)
{ {
if (element[j] != '.') continue; if (element[aux::numeric_cast<std::size_t>(j)] != '.') continue;
dot = j; dot = j;
break; break;
} }
@ -470,7 +471,7 @@ namespace libtorrent
if (p && p.list_size() > 0) if (p && p.list_size() > 0)
{ {
std::size_t const preallocate = path.size() + path_length(p, ec); std::size_t const preallocate = path.size() + std::size_t(path_length(p, ec));
if (ec) return false; if (ec) return false;
path.reserve(preallocate); path.reserve(preallocate);
@ -515,7 +516,7 @@ namespace libtorrent
{ {
if (bdecode_node s_p = dict.dict_find_list("symlink path")) if (bdecode_node s_p = dict.dict_find_list("symlink path"))
{ {
int const preallocate = path_length(s_p, ec); std::size_t const preallocate = std::size_t(path_length(s_p, ec));
if (ec) return false; if (ec) return false;
symlink_path.reserve(preallocate); symlink_path.reserve(preallocate);
for (int i = 0, end(s_p.list_size()); i < end; ++i) for (int i = 0, end(s_p.list_size()); i < end; ++i)
@ -531,8 +532,8 @@ namespace libtorrent
} }
if (filename_len > int(path.length()) if (filename_len > int(path.length())
|| path.compare(path.size() - filename_len, filename_len, filename || path.compare(path.size() - std::size_t(filename_len), std::size_t(filename_len), filename
, filename_len) != 0) , std::size_t(filename_len)) != 0)
{ {
// if the filename was sanitized and differ, clear it to just use path // if the filename was sanitized and differ, clear it to just use path
filename = nullptr; filename = nullptr;
@ -549,7 +550,7 @@ namespace libtorrent
std::size_t operator()(std::string const& s) const std::size_t operator()(std::string const& s) const
{ {
char const* s1 = s.c_str(); char const* s1 = s.c_str();
size_t ret = 5381; std::size_t ret = 5381;
int c; int c;
for (;;) for (;;)
@ -557,7 +558,7 @@ namespace libtorrent
c = *s1++; c = *s1++;
if (c == 0) if (c == 0)
break; break;
ret = (ret * 33) ^ to_lower(char(c)); ret = (ret * 33) ^ std::size_t(to_lower(char(c)));
} }
return ret; return ret;
@ -659,7 +660,7 @@ namespace libtorrent
TORRENT_ASSERT(m_piece_hashes); TORRENT_ASSERT(m_piece_hashes);
m_info_section.reset(new char[m_info_section_size]); m_info_section.reset(new char[m_info_section_size]);
std::memcpy(m_info_section.get(), t.m_info_section.get(), m_info_section_size); std::memcpy(m_info_section.get(), t.m_info_section.get(), aux::numeric_cast<std::size_t>(m_info_section_size));
ptrdiff_t offset = m_info_section.get() - t.m_info_section.get(); ptrdiff_t offset = m_info_section.get() - t.m_info_section.get();
@ -722,7 +723,7 @@ namespace libtorrent
std::unordered_set<std::string, string_hash_no_case, string_eq_no_case> files; std::unordered_set<std::string, string_hash_no_case, string_eq_no_case> files;
std::vector<std::string> const& paths = m_files.paths(); std::vector<std::string> const& paths = m_files.paths();
files.reserve(paths.size() + m_files.num_files()); files.reserve(paths.size() + aux::numeric_cast<std::size_t>(m_files.num_files()));
// insert all directories first, to make sure no files // insert all directories first, to make sure no files
// are allowed to collied with them // are allowed to collied with them
@ -1053,9 +1054,9 @@ namespace libtorrent
// copy the info section // copy the info section
m_info_section_size = int(section.size()); m_info_section_size = int(section.size());
m_info_section.reset(new char[m_info_section_size]); m_info_section.reset(new char[m_info_section_size]);
std::memcpy(m_info_section.get(), section.data(), m_info_section_size); std::memcpy(m_info_section.get(), section.data(), aux::numeric_cast<std::size_t>(m_info_section_size));
TORRENT_ASSERT(section[0] == 'd'); TORRENT_ASSERT(section[0] == 'd');
TORRENT_ASSERT(section[m_info_section_size - 1] == 'e'); TORRENT_ASSERT(section[aux::numeric_cast<std::size_t>(m_info_section_size - 1)] == 'e');
// when translating a pointer that points into the 'info' tree's // when translating a pointer that points into the 'info' tree's
// backing buffer, into a pointer to our copy of the info section, // backing buffer, into a pointer to our copy of the info section,
@ -1157,7 +1158,7 @@ namespace libtorrent
m_files.set_piece_length(0); m_files.set_piece_length(0);
return false; return false;
} }
if (files.num_pieces() >= std::numeric_limits<int>::max()/2) if (files.num_pieces() >= std::numeric_limits<int>::max() / 2)
{ {
ec = errors::too_many_pieces_in_torrent; ec = errors::too_many_pieces_in_torrent;
// mark the torrent as invalid // mark the torrent as invalid
@ -1168,7 +1169,7 @@ namespace libtorrent
int const num_nodes = merkle_num_nodes(num_leafs); int const num_nodes = merkle_num_nodes(num_leafs);
m_merkle_first_leaf = num_nodes - num_leafs; m_merkle_first_leaf = num_nodes - num_leafs;
m_merkle_tree.resize(num_nodes); m_merkle_tree.resize(num_nodes);
std::memset(&m_merkle_tree[0], 0, num_nodes * 20); std::memset(m_merkle_tree.data(), 0, aux::numeric_cast<std::size_t>(num_nodes * 20));
m_merkle_tree[0].assign(root_hash.string_ptr()); m_merkle_tree[0].assign(root_hash.string_ptr());
} }
@ -1293,7 +1294,7 @@ namespace libtorrent
ret[sibling] = m_merkle_tree[sibling]; ret[sibling] = m_merkle_tree[sibling];
// we cannot build the tree path if one // we cannot build the tree path if one
// of the nodes in the tree is missing // of the nodes in the tree is missing
TORRENT_ASSERT(m_merkle_tree[sibling] != sha1_hash(nullptr)); TORRENT_ASSERT(!m_merkle_tree[sibling].is_all_zeros());
n = parent; n = parent;
} }
return ret; return ret;
@ -1361,7 +1362,7 @@ namespace libtorrent
if (str.type() != bdecode_node::string_t) continue; if (str.type() != bdecode_node::string_t) continue;
m_owned_collections.push_back(std::string(str.string_ptr() m_owned_collections.push_back(std::string(str.string_ptr()
, str.string_length())); , aux::numeric_cast<std::size_t>(str.string_length())));
} }
} }
#endif // TORRENT_DISABLE_MUTABLE_TORRENTS #endif // TORRENT_DISABLE_MUTABLE_TORRENTS
@ -1442,7 +1443,7 @@ namespace libtorrent
std::int64_t cd = torrent_file.dict_find_int_value("creation date", -1); std::int64_t cd = torrent_file.dict_find_int_value("creation date", -1);
if (cd >= 0) if (cd >= 0)
{ {
m_creation_date = long(cd); m_creation_date = std::time_t(cd);
} }
// if there are any url-seeds, extract them // if there are any url-seeds, extract them
@ -1606,7 +1607,7 @@ namespace libtorrent
ret.reserve(m_collections.size() + m_owned_collections.size()); ret.reserve(m_collections.size() + m_owned_collections.size());
for (auto const& c : m_collections) for (auto const& c : m_collections)
ret.push_back(std::string(c.first, c.second)); ret.push_back(std::string(c.first, aux::numeric_cast<std::size_t>(c.second)));
for (auto const& c : m_owned_collections) for (auto const& c : m_owned_collections)
ret.push_back(c); ret.push_back(c);