From 0f7a55cb8b9f0e6cdba87ee7bb187a563016d056 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Mon, 9 Jan 2017 01:43:57 -0500 Subject: [PATCH] fixing sign-conversion warnings, part 1 (#1503) fixing sign-conversion warnings, part 1 --- include/libtorrent/aux_/suggest_piece.hpp | 3 ++- include/libtorrent/aux_/vector.hpp | 22 ++++++++++++++-------- include/libtorrent/bencode.hpp | 11 ++++++++--- include/libtorrent/bt_peer_connection.hpp | 2 +- include/libtorrent/lazy_entry.hpp | 8 ++++---- include/libtorrent/utp_socket_manager.hpp | 4 ++-- src/allocator.cpp | 8 ++++---- src/assert.cpp | 2 +- src/chained_buffer.cpp | 6 +++--- src/crc32c.cpp | 2 +- src/http_tracker_connection.cpp | 4 ++-- 11 files changed, 42 insertions(+), 30 deletions(-) diff --git a/include/libtorrent/aux_/suggest_piece.hpp b/include/libtorrent/aux_/suggest_piece.hpp index aae5c85a6..c95455824 100644 --- a/include/libtorrent/aux_/suggest_piece.hpp +++ b/include/libtorrent/aux_/suggest_piece.hpp @@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/bitfield.hpp" #include "libtorrent/sliding_average.hpp" +#include "libtorrent/aux_/vector.hpp" namespace libtorrent { namespace aux { @@ -117,7 +118,7 @@ private: // read from disk (and are likely in our read cache). // pieces closer to the end were inserted into the cache more recently and // have higher priority - std::vector m_priority_pieces; + vector m_priority_pieces; sliding_average<30> m_availability; }; diff --git a/include/libtorrent/aux_/vector.hpp b/include/libtorrent/aux_/vector.hpp index 696abad6b..3c640dcb4 100644 --- a/include/libtorrent/aux_/vector.hpp +++ b/include/libtorrent/aux_/vector.hpp @@ -35,15 +35,22 @@ POSSIBILITY OF SUCH DAMAGE. #include +#include "libtorrent/units.hpp" #include "libtorrent/assert.hpp" namespace libtorrent { namespace aux { + template + struct underlying_index_t { using type = T; }; + + template + struct underlying_index_t> { using type = U; }; + template struct vector : std::vector { using base = std::vector; - using underlying_index = typename IndexType::underlying_type; + using underlying_index = typename underlying_index_t::type; // pull in constructors from base class using base::base; @@ -52,14 +59,14 @@ namespace libtorrent { namespace aux { { TORRENT_ASSERT(idx >= IndexType(0)); TORRENT_ASSERT(idx < end_index()); - return this->base::operator[](static_cast(idx)); + return this->base::operator[](std::size_t(static_cast(idx))); } auto operator[](IndexType idx) -> decltype(this->base::operator[](underlying_index())) { TORRENT_ASSERT(idx >= IndexType(0)); TORRENT_ASSERT(idx < end_index()); - return this->base::operator[](static_cast(idx)); + return this->base::operator[](std::size_t(static_cast(idx))); } IndexType end_index() const @@ -78,19 +85,18 @@ namespace libtorrent { namespace aux { iterator_range range(vector& vec , IndexType begin, IndexType end) { - using type = typename IndexType::underlying_type; - return { vec.data() + static_cast(begin), vec.data() + static_cast(end) }; + using type = typename underlying_index_t::type; + return {vec.data() + static_cast(begin), vec.data() + static_cast(end)}; } template iterator_range range(vector const& vec , IndexType begin, IndexType end) { - using type = typename IndexType::underlying_type; - return { vec.data() + static_cast(begin), vec.data() + static_cast(end) }; + using type = typename underlying_index_t::type; + return {vec.data() + static_cast(begin), vec.data() + static_cast(end)}; } }} #endif - diff --git a/include/libtorrent/bencode.hpp b/include/libtorrent/bencode.hpp index 926ddaaa7..18701c843 100644 --- a/include/libtorrent/bencode.hpp +++ b/include/libtorrent/bencode.hpp @@ -86,13 +86,17 @@ namespace libtorrent namespace detail { - template - int write_integer(OutIt& out, entry::integer_type val) + template ::value>::type> + int write_integer(OutIt& out, In data) { + entry::integer_type const val(data); + TORRENT_ASSERT(data == In(val)); // the stack allocated buffer for keeping the // decimal representation of the number can // not hold number bigger than this: static_assert(sizeof(entry::integer_type) <= 8, "64 bit integers required"); + static_assert(sizeof(data) <= sizeof(entry::integer_type), "input data too big, see entry::integer_type"); char buf[21]; int ret = 0; for (char const* str = integer_to_str(buf, 21, val); @@ -328,7 +332,8 @@ namespace libtorrent // ---------------------------------------------- // string default: - if (is_digit(std::uint8_t(*in))) + static_assert(sizeof(*in) == 1, "Input iterator to 8 bit data required"); + if (is_digit(char(*in))) { std::string len_s = read_until(in, end, ':', err); if (err) diff --git a/include/libtorrent/bt_peer_connection.hpp b/include/libtorrent/bt_peer_connection.hpp index 2facf40cd..fbd599e0a 100644 --- a/include/libtorrent/bt_peer_connection.hpp +++ b/include/libtorrent/bt_peer_connection.hpp @@ -323,7 +323,7 @@ namespace libtorrent // if we're encrypting this buffer, we need to make a copy // since we'll mutate it std::unique_ptr buf(new char[size]); - std::memcpy(buf.get(), buffer.get(), size); + std::copy(buffer.get(), buffer.get() + size, buf.get()); append_send_buffer(std::move(buf), size); } else diff --git a/include/libtorrent/lazy_entry.hpp b/include/libtorrent/lazy_entry.hpp index 727205aca..5c65fd3ee 100644 --- a/include/libtorrent/lazy_entry.hpp +++ b/include/libtorrent/lazy_entry.hpp @@ -120,7 +120,7 @@ namespace libtorrent // with memcmp. bool operator<(pascal_string const& rhs) const { - return std::memcmp(ptr, rhs.ptr, (std::min)(len, rhs.len)) < 0 + return std::memcmp(ptr, rhs.ptr, std::size_t((std::min)(len, rhs.len))) < 0 || len < rhs.len; } }; @@ -161,9 +161,9 @@ namespace libtorrent TORRENT_ASSERT(m_type == none_t); m_type = int_t; m_data.start = start; - m_size = length; + m_size = std::uint32_t(length); m_begin = start - 1; // include 'i' - m_len = length + 2; // include 'e' + m_len = std::uint32_t(length + 2); // include 'e' } // requires the type to be an integer. return the integer value @@ -321,7 +321,7 @@ namespace libtorrent { TORRENT_ASSERT(end > m_begin); TORRENT_ASSERT(end - m_begin < (std::numeric_limits::max)()); - m_len = int(end - m_begin); + m_len = std::uint32_t(end - m_begin); } // internal diff --git a/include/libtorrent/utp_socket_manager.hpp b/include/libtorrent/utp_socket_manager.hpp index 79ca9789f..9985e2b82 100644 --- a/include/libtorrent/utp_socket_manager.hpp +++ b/include/libtorrent/utp_socket_manager.hpp @@ -106,8 +106,8 @@ namespace libtorrent void restrict_mtu(int mtu) { - m_restrict_mtu[m_mtu_idx] = mtu; - m_mtu_idx = (m_mtu_idx + 1) % m_restrict_mtu.size(); + m_restrict_mtu[std::size_t(m_mtu_idx)] = mtu; + m_mtu_idx = (m_mtu_idx + 1) % int(m_restrict_mtu.size()); } int restrict_mtu() const diff --git a/src/allocator.cpp b/src/allocator.cpp index 651ab8afd..f0fef8b6f 100644 --- a/src/allocator.cpp +++ b/src/allocator.cpp @@ -140,8 +140,8 @@ namespace libtorrent #define mprotect(buf, size, prot) VirtualProtect(buf, size, prot, nullptr) #define PROT_READ PAGE_READONLY #endif - mprotect(ret, page, PROT_READ); - mprotect(static_cast(ret) + (num_pages-1) * page, page, PROT_READ); + mprotect(ret, std::size_t(page), PROT_READ); + mprotect(static_cast(ret) + (num_pages-1) * page, std::size_t(page), PROT_READ); #ifdef TORRENT_WINDOWS #undef mprotect @@ -168,11 +168,11 @@ namespace libtorrent #endif int const page = page_size(); // make the two surrounding pages non-readable and -writable - mprotect(block - page, page, PROT_READ | PROT_WRITE); + mprotect(block - page, std::size_t(page), PROT_READ | PROT_WRITE); alloc_header* h = reinterpret_cast(block - page); int const num_pages = int((h->size + (page - 1)) / page + 2); TORRENT_ASSERT(h->magic == 0x1337); - mprotect(block + (num_pages - 2) * page, page, PROT_READ | PROT_WRITE); + mprotect(block + (num_pages - 2) * page, std::size_t(page), PROT_READ | PROT_WRITE); // std::fprintf(stderr, "free: %p head: %p tail: %p size: %d\n", block, block - page, block + h->size, int(h->size)); h->magic = 0; block -= page; diff --git a/src/assert.cpp b/src/assert.cpp index c9133dc4a..e4b114922 100644 --- a/src/assert.cpp +++ b/src/assert.cpp @@ -134,7 +134,7 @@ TORRENT_EXPORT void print_backtrace(char* out, int len, int max_depth, void*) for (int i = 1; i < size && len > 0; ++i) { - int ret = std::snprintf(out, len, "%d: %s\n", i, demangle(symbols[i]).c_str()); + int ret = std::snprintf(out, std::size_t(len), "%d: %s\n", i, demangle(symbols[i]).c_str()); out += ret; len -= ret; if (i - 1 == max_depth && max_depth > 0) break; diff --git a/src/chained_buffer.cpp b/src/chained_buffer.cpp index 8ab8e9cf5..08ddbd399 100644 --- a/src/chained_buffer.cpp +++ b/src/chained_buffer.cpp @@ -90,7 +90,7 @@ namespace libtorrent TORRENT_ASSERT(!m_destructed); char* const insert = allocate_appendix(s); if (insert == nullptr) return nullptr; - std::memcpy(insert, buf, s); + std::memcpy(insert, buf, std::size_t(s)); return insert; } @@ -137,11 +137,11 @@ namespace libtorrent if (i->used_size > bytes) { TORRENT_ASSERT(bytes > 0); - vec.push_back(Buffer(i->buf, bytes)); + vec.push_back(Buffer(i->buf, std::size_t(bytes))); break; } TORRENT_ASSERT(i->used_size > 0); - vec.push_back(Buffer(i->buf, i->used_size)); + vec.push_back(Buffer(i->buf, std::size_t(i->used_size))); bytes -= i->used_size; } } diff --git a/src/crc32c.cpp b/src/crc32c.cpp index 09f99ee4c..c1213efe4 100644 --- a/src/crc32c.cpp +++ b/src/crc32c.cpp @@ -142,7 +142,7 @@ namespace libtorrent #endif boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc; - crc.process_bytes(buf, num_words * 8); + crc.process_bytes(buf, std::size_t(num_words * 8)); return crc.checksum(); } } diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index 1da10497a..38bef9abb 100644 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -519,8 +519,8 @@ namespace libtorrent } else if (peers_ent && peers_ent.type() == bdecode_node::list_t) { - int len = peers_ent.list_size(); - resp.peers.reserve(len); + int const len = peers_ent.list_size(); + resp.peers.reserve(std::size_t(len)); error_code parse_error; for (int i = 0; i < len; ++i) {