From d36c598eeb1ce289b50fc28a9002d26bfd65b15c Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Mon, 30 Jan 2017 20:31:32 -0500 Subject: [PATCH] fixing sign-conversion warnings, part 10 (#1642) fixing sign-conversion warnings, part 10 --- src/block_cache.cpp | 5 ++-- src/bt_peer_connection.cpp | 56 +++++++++++++++++++++----------------- src/disk_io_thread.cpp | 35 ++++++++++++------------ 3 files changed, 52 insertions(+), 44 deletions(-) diff --git a/src/block_cache.cpp b/src/block_cache.cpp index 3070f20bc..eec986e81 100644 --- a/src/block_cache.cpp +++ b/src/block_cache.cpp @@ -42,6 +42,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/performance_counters.hpp" #include "libtorrent/aux_/time.hpp" #include "libtorrent/aux_/block_cache_reference.hpp" +#include "libtorrent/aux_/numeric_cast.hpp" /* @@ -620,7 +621,7 @@ cached_piece_entry* block_cache::allocate_piece(disk_io_job const* j, std::uint1 pe.piece = j->piece; pe.storage = j->storage; pe.expire = aux::time_now(); - pe.blocks_in_piece = blocks_in_piece; + pe.blocks_in_piece = aux::numeric_cast(blocks_in_piece); pe.blocks.reset(new (std::nothrow) cached_block_entry[blocks_in_piece]); if (!pe.blocks) return nullptr; @@ -1772,7 +1773,7 @@ int block_cache::copy_from_piece(cached_piece_entry* const pe - block_offset, size); std::memcpy(j->buffer.disk_block + buffer_offset , pe->blocks[block].buf + block_offset - , to_copy); + , aux::numeric_cast(to_copy)); size -= to_copy; block_offset = 0; buffer_offset += to_copy; diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 2765ce381..3161c6750 100644 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -531,7 +531,7 @@ namespace libtorrent return; } - int const pad_size = random(512); + int const pad_size = int(random(512)); #ifndef TORRENT_DISABLE_LOGGING peer_log(peer_log_alert::info, "ENCRYPTION", "pad size: %d", pad_size); @@ -570,7 +570,7 @@ namespace libtorrent key_t const secret_key = m_dh_key_exchange->get_secret(); std::array const secret = export_key(secret_key); - int const pad_size = random(512); + int const pad_size = int(random(512)); // synchash,skeyhash,vc,crypto_provide,len(pad),pad,len(ia) char msg[20 + 20 + 8 + 4 + 2 + 512 + 2]; @@ -619,7 +619,7 @@ namespace libtorrent m_dh_key_exchange.reset(); // secret should be invalid at this point // write the verification constant and crypto field - int const encrypt_size = sizeof(msg) - 512 + pad_size - 40; + int const encrypt_size = int(sizeof(msg)) - 512 + pad_size - 40; // this is an invalid setting, but let's just make the best of the situation int const enc_level = m_settings.get_int(settings_pack::allowed_enc_level); @@ -634,9 +634,9 @@ namespace libtorrent #endif write_pe_vc_cryptofield(ptr, encrypt_size, crypto_provide, pad_size); - span vec(ptr, encrypt_size); + span vec(ptr, aux::numeric_cast(encrypt_size)); m_rc4->encrypt(vec); - send_buffer(msg, sizeof(msg) - 512 + pad_size); + send_buffer(msg, int(sizeof(msg)) - 512 + pad_size); } void bt_peer_connection::write_pe4_sync(int crypto_select) @@ -649,13 +649,13 @@ namespace libtorrent TORRENT_ASSERT(crypto_select == 0x02 || crypto_select == 0x01); TORRENT_ASSERT(!m_sent_handshake); - int const pad_size = random(512); + int const pad_size = int(random(512)); int const buf_size = 8 + 4 + 2 + pad_size; char msg[512 + 8 + 4 + 2]; write_pe_vc_cryptofield(msg, sizeof(msg), crypto_select, pad_size); - span vec(msg, buf_size); + span vec(msg, aux::numeric_cast(buf_size)); m_rc4->encrypt(vec); send_buffer(msg, buf_size); @@ -1335,7 +1335,7 @@ namespace libtorrent span recv_buffer = m_recv_buffer.get(); const char* ptr = recv_buffer.begin() + 1; - piece_index_t const piece(detail::read_uint32(ptr)); + piece_index_t const piece(detail::read_int32(ptr)); incoming_suggest(piece); } @@ -1726,7 +1726,7 @@ namespace libtorrent #endif return; } - piece_index_t const piece(aux::read_uint32(recv_buffer)); + piece_index_t const piece(aux::numeric_cast(aux::read_uint32(recv_buffer))); incoming_dont_have(piece); return; } @@ -2142,7 +2142,7 @@ namespace libtorrent } else { - std::memset(ptr, 0, packet_size - 5); + std::memset(ptr, 0, aux::numeric_cast(packet_size - 5)); piece_picker const& p = t->picker(); int mask = 0x80; for (piece_index_t i(0); i < piece_index_t(num_pieces); ++i) @@ -2452,7 +2452,7 @@ namespace libtorrent // back-patch the length field char* ptr2 = msg; - detail::write_int32(int(r.length + 1 + 4 + 4 + 4 + piece_list_buf.size()) + detail::write_int32(r.length + 1 + 4 + 4 + 4 + int(piece_list_buf.size()) , ptr2); send_buffer(msg, 17); @@ -2502,9 +2502,9 @@ namespace libtorrent { int const consumed = m_enc_handler.decrypt(m_recv_buffer, bytes_transferred); #ifndef TORRENT_DISABLE_LOGGING - if (consumed + bytes_transferred > 0) + if (consumed + int(bytes_transferred) > 0) peer_log(peer_log_alert::incoming_message, "ENCRYPTION" - , "decrypted block s = %d", int(consumed + bytes_transferred)); + , "decrypted block s = %d", consumed + int(bytes_transferred)); #endif if (bytes_transferred == SIZE_MAX) { @@ -2529,9 +2529,9 @@ namespace libtorrent std::int64_t cur_payload_dl = m_statistics.last_payload_downloaded(); std::int64_t cur_protocol_dl = m_statistics.last_protocol_downloaded(); #endif - on_receive_impl(sub_transferred); - bytes_transferred -= sub_transferred; TORRENT_ASSERT(sub_transferred > 0); + on_receive_impl(std::size_t(sub_transferred)); + bytes_transferred -= std::size_t(sub_transferred); #if TORRENT_USE_ASSERTS TORRENT_ASSERT(m_statistics.last_payload_downloaded() - cur_payload_dl >= 0); @@ -2692,10 +2692,13 @@ namespace libtorrent m_state = state_t::read_pe_skey_vc; // skey,vc - 28 bytes m_sync_hash.reset(); - int transferred_used = int(bytes_processed - int(recv_buffer.size()) + bytes_transferred); + int const transferred_used = bytes_processed + - aux::numeric_cast(recv_buffer.size()) + + aux::numeric_cast(bytes_transferred); + TORRENT_ASSERT(transferred_used >= 0); TORRENT_ASSERT(transferred_used <= int(bytes_transferred)); received_bytes(0, transferred_used); - bytes_transferred -= transferred_used; + bytes_transferred -= std::size_t(transferred_used); m_recv_buffer.cut(bytes_processed, 28); } } @@ -2827,10 +2830,13 @@ namespace libtorrent , "sync point (verification constant) found at offset %d" , m_sync_bytes_read + bytes_processed - 8); #endif - int transferred_used = int(bytes_processed - int(recv_buffer.size()) + bytes_transferred); + int const transferred_used = bytes_processed + - aux::numeric_cast(recv_buffer.size()) + + aux::numeric_cast(bytes_transferred); + TORRENT_ASSERT(transferred_used >= 0); TORRENT_ASSERT(transferred_used <= int(bytes_transferred)); received_bytes(0, transferred_used); - bytes_transferred -= transferred_used; + bytes_transferred -= std::size_t(transferred_used); m_recv_buffer.cut(bytes_processed, 4 + 2); @@ -2967,7 +2973,7 @@ namespace libtorrent if (!is_outgoing()) { - recv_buffer = recv_buffer.subspan(pad_size); + recv_buffer = recv_buffer.subspan(aux::numeric_cast(pad_size)); int len_ia = aux::read_int16(recv_buffer); if (len_ia < 0) @@ -3052,7 +3058,7 @@ namespace libtorrent if (m_rc4_encrypted) { span const remaining = m_recv_buffer.mutable_buffer() - .subspan(m_recv_buffer.packet_size()); + .subspan(aux::numeric_cast(m_recv_buffer.packet_size())); rc4_decrypt(remaining); #ifndef TORRENT_DISABLE_LOGGING @@ -3182,9 +3188,9 @@ namespace libtorrent #ifndef TORRENT_DISABLE_LOGGING std::string extensions; extensions.resize(8 * 8); - for (int i=0; i < 8; ++i) + for (std::size_t i = 0; i < 8; ++i) { - for (int j=0; j < 8; ++j) + for (std::size_t j = 0; j < 8; ++j) { if (recv_buffer[i] & (0x80 >> j)) extensions[i * 8 + j] = '1'; else extensions[i * 8 + j] = '0'; @@ -3283,7 +3289,7 @@ namespace libtorrent hex_pid[40] = 0; char ascii_pid[21]; ascii_pid[20] = 0; - for (int i = 0; i != 20; ++i) + for (std::size_t i = 0; i != 20; ++i) { if (is_print(recv_buffer[i])) ascii_pid[i] = recv_buffer[i]; else ascii_pid[i] = '.'; @@ -3420,7 +3426,7 @@ namespace libtorrent TORRENT_ASSERT(bytes_transferred <= 5); int used_bytes = int(recv_buffer.size()) > 4 ? int(bytes_transferred) - 1: int(bytes_transferred); received_bytes(0, used_bytes); - bytes_transferred -= used_bytes; + bytes_transferred -= aux::numeric_cast(used_bytes); if (int(recv_buffer.size()) < 4) return; TORRENT_ASSERT(bytes_transferred <= 1); diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 0984bf60d..503bea6ce 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -583,9 +583,9 @@ namespace libtorrent int piece_size = pe->storage->files()->piece_size(pe->piece); TORRENT_PIECE_ASSERT(piece_size > 0, pe); - int iov_len = 0; + std::size_t iov_len = 0; // the blocks we're flushing - int num_flushing = 0; + std::size_t num_flushing = 0; #if DEBUG_DISK_THREAD DLOG("build_iov: piece: %d [", int(pe->piece)); @@ -618,7 +618,7 @@ namespace libtorrent flushing[num_flushing++] = i + block_base_index; iov[iov_len].iov_base = pe->blocks[i].buf; - iov[iov_len].iov_len = std::min(block_size, size_left); + iov[iov_len].iov_len = aux::numeric_cast(std::min(block_size, size_left)); ++iov_len; pe->blocks[i].pending = true; @@ -627,7 +627,7 @@ namespace libtorrent DLOG("]\n"); TORRENT_PIECE_ASSERT(iov_len == num_flushing, pe); - return iov_len; + return aux::numeric_cast(iov_len); } // does the actual writing to disk @@ -656,13 +656,14 @@ namespace libtorrent // issue the actual write operation auto iov_start = iov; - int flushing_start = 0; + std::size_t flushing_start = 0; piece_index_t const piece = pe->piece; int const blocks_in_piece = pe->blocks_in_piece; bool failed = false; - for (int i = 1; i <= num_blocks; ++i) + std::size_t const n_blocks = aux::numeric_cast(num_blocks); + for (std::size_t i = 1; i <= n_blocks; ++i) { - if (i < num_blocks && flushing[i] == flushing[i - 1] + 1) continue; + if (i < n_blocks && flushing[i] == flushing[i - 1] + 1) continue; int const ret = pe->storage->writev( iov_start.first(i - flushing_start) , piece_index_t(static_cast(piece) + flushing[flushing_start] / blocks_in_piece) @@ -919,7 +920,7 @@ namespace libtorrent DLOG("try_flush_write_blocks: %d\n", num); list_iterator range = m_disk_cache.write_lru_pieces(); - std::vector> pieces; + aux::vector> pieces; pieces.reserve(m_disk_cache.num_write_lru_pieces()); for (list_iterator p = range; p.get() && num > 0; p.next()) @@ -1283,8 +1284,8 @@ namespace libtorrent // if this is the last piece, adjust the size of the // last buffer to match up - iov[iov_len - 1].iov_len = std::min(int(piece_size - adjusted_offset) - - (iov_len - 1) * block_size, block_size); + iov[iov_len - 1].iov_len = aux::numeric_cast(std::min(int(piece_size - adjusted_offset) + - (iov_len - 1) * block_size, block_size)); TORRENT_ASSERT(iov[iov_len - 1].iov_len > 0); // at this point, all the buffers are allocated and iov is initialized @@ -1665,7 +1666,7 @@ namespace libtorrent bool exceeded = false; disk_buffer_holder buffer(*this, m_disk_cache.allocate_buffer(exceeded, o, "receive buffer")); if (!buffer) aux::throw_ex(); - std::memcpy(buffer.get(), buf, r.length); + std::memcpy(buffer.get(), buf, aux::numeric_cast(r.length)); disk_io_job* j = allocate_job(disk_io_job::write); j->storage = m_torrents[storage]->shared_from_this(); @@ -2154,7 +2155,7 @@ namespace libtorrent time_point const start_time = clock_type::now(); - iov.iov_len = std::min(block_size, piece_size - offset); + iov.iov_len = aux::numeric_cast(std::min(block_size, piece_size - offset)); ret = j->storage->readv(iov, j->piece , offset, file_flags, j->error); if (ret < 0) break; @@ -2307,7 +2308,7 @@ namespace libtorrent for (int i = offset / block_size; i < blocks_in_piece; ++i) { iovec_t iov; - iov.iov_len = std::min(block_size, piece_size - offset); + iov.iov_len = aux::numeric_cast(std::min(block_size, piece_size - offset)); if (next_locked_block < num_locked_blocks && locked_blocks[next_locked_block] == i) @@ -2576,9 +2577,9 @@ namespace libtorrent ? cached_piece_info::volatile_read_cache : cached_piece_info::read_cache; int blocks_in_piece = i->blocks_in_piece; - info.blocks.resize(blocks_in_piece); + info.blocks.resize(aux::numeric_cast(blocks_in_piece)); for (int b = 0; b < blocks_in_piece; ++b) - info.blocks[b] = i->blocks[b].buf != nullptr; + info.blocks[std::size_t(b)] = i->blocks[b].buf != nullptr; } } // anonymous namespace @@ -2663,7 +2664,7 @@ namespace libtorrent { std::shared_ptr storage = m_torrents[st]; TORRENT_ASSERT(storage); - ret->pieces.reserve(storage->num_pieces()); + ret->pieces.reserve(aux::numeric_cast(storage->num_pieces())); for (auto pe : storage->cached_pieces()) { @@ -2678,7 +2679,7 @@ namespace libtorrent } else { - ret->pieces.reserve(m_disk_cache.num_pieces()); + ret->pieces.reserve(aux::numeric_cast(m_disk_cache.num_pieces())); auto range = m_disk_cache.all_pieces(); for (auto i = range.first; i != range.second; ++i)