fixing sign-conversion warnings, part 1 (#1503)

fixing sign-conversion warnings, part 1
This commit is contained in:
Alden Torres 2017-01-09 01:43:57 -05:00 committed by Arvid Norberg
parent 1d1ab4f4b5
commit 0f7a55cb8b
11 changed files with 42 additions and 30 deletions

View File

@ -38,6 +38,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/bitfield.hpp" #include "libtorrent/bitfield.hpp"
#include "libtorrent/sliding_average.hpp" #include "libtorrent/sliding_average.hpp"
#include "libtorrent/aux_/vector.hpp"
namespace libtorrent { namespace aux { namespace libtorrent { namespace aux {
@ -117,7 +118,7 @@ private:
// read from disk (and are likely in our read cache). // read from disk (and are likely in our read cache).
// pieces closer to the end were inserted into the cache more recently and // pieces closer to the end were inserted into the cache more recently and
// have higher priority // have higher priority
std::vector<piece_index_t> m_priority_pieces; vector<piece_index_t, int> m_priority_pieces;
sliding_average<30> m_availability; sliding_average<30> m_availability;
}; };

View File

@ -35,15 +35,22 @@ POSSIBILITY OF SUCH DAMAGE.
#include <vector> #include <vector>
#include "libtorrent/units.hpp"
#include "libtorrent/assert.hpp" #include "libtorrent/assert.hpp"
namespace libtorrent { namespace aux { namespace libtorrent { namespace aux {
template <typename T>
struct underlying_index_t { using type = T; };
template <typename U, typename Tag>
struct underlying_index_t<aux::strong_typedef<U, Tag>> { using type = U; };
template <typename T, typename IndexType> template <typename T, typename IndexType>
struct vector : std::vector<T> struct vector : std::vector<T>
{ {
using base = std::vector<T>; using base = std::vector<T>;
using underlying_index = typename IndexType::underlying_type; using underlying_index = typename underlying_index_t<IndexType>::type;
// pull in constructors from base class // pull in constructors from base class
using base::base; using base::base;
@ -52,14 +59,14 @@ namespace libtorrent { namespace aux {
{ {
TORRENT_ASSERT(idx >= IndexType(0)); TORRENT_ASSERT(idx >= IndexType(0));
TORRENT_ASSERT(idx < end_index()); TORRENT_ASSERT(idx < end_index());
return this->base::operator[](static_cast<underlying_index>(idx)); return this->base::operator[](std::size_t(static_cast<underlying_index>(idx)));
} }
auto operator[](IndexType idx) -> decltype(this->base::operator[](underlying_index())) auto operator[](IndexType idx) -> decltype(this->base::operator[](underlying_index()))
{ {
TORRENT_ASSERT(idx >= IndexType(0)); TORRENT_ASSERT(idx >= IndexType(0));
TORRENT_ASSERT(idx < end_index()); TORRENT_ASSERT(idx < end_index());
return this->base::operator[](static_cast<underlying_index>(idx)); return this->base::operator[](std::size_t(static_cast<underlying_index>(idx)));
} }
IndexType end_index() const IndexType end_index() const
@ -78,19 +85,18 @@ namespace libtorrent { namespace aux {
iterator_range<T*> range(vector<T, IndexType>& vec iterator_range<T*> range(vector<T, IndexType>& vec
, IndexType begin, IndexType end) , IndexType begin, IndexType end)
{ {
using type = typename IndexType::underlying_type; using type = typename underlying_index_t<IndexType>::type;
return { vec.data() + static_cast<type>(begin), vec.data() + static_cast<type>(end) }; return {vec.data() + static_cast<type>(begin), vec.data() + static_cast<type>(end)};
} }
template <typename T, typename IndexType> template <typename T, typename IndexType>
iterator_range<T const*> range(vector<T, IndexType> const& vec iterator_range<T const*> range(vector<T, IndexType> const& vec
, IndexType begin, IndexType end) , IndexType begin, IndexType end)
{ {
using type = typename IndexType::underlying_type; using type = typename underlying_index_t<IndexType>::type;
return { vec.data() + static_cast<type>(begin), vec.data() + static_cast<type>(end) }; return {vec.data() + static_cast<type>(begin), vec.data() + static_cast<type>(end)};
} }
}} }}
#endif #endif

View File

@ -86,13 +86,17 @@ namespace libtorrent
namespace detail namespace detail
{ {
template <class OutIt> template <class OutIt, class In, typename Cond
int write_integer(OutIt& out, entry::integer_type val) = typename std::enable_if<std::is_integral<In>::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 // the stack allocated buffer for keeping the
// decimal representation of the number can // decimal representation of the number can
// not hold number bigger than this: // not hold number bigger than this:
static_assert(sizeof(entry::integer_type) <= 8, "64 bit integers required"); 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]; char buf[21];
int ret = 0; int ret = 0;
for (char const* str = integer_to_str(buf, 21, val); for (char const* str = integer_to_str(buf, 21, val);
@ -328,7 +332,8 @@ namespace libtorrent
// ---------------------------------------------- // ----------------------------------------------
// string // string
default: 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); std::string len_s = read_until(in, end, ':', err);
if (err) if (err)

View File

@ -323,7 +323,7 @@ namespace libtorrent
// if we're encrypting this buffer, we need to make a copy // if we're encrypting this buffer, we need to make a copy
// since we'll mutate it // since we'll mutate it
std::unique_ptr<char[]> buf(new char[size]); std::unique_ptr<char[]> 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); append_send_buffer(std::move(buf), size);
} }
else else

View File

@ -120,7 +120,7 @@ namespace libtorrent
// with memcmp. // with memcmp.
bool operator<(pascal_string const& rhs) const 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; || len < rhs.len;
} }
}; };
@ -161,9 +161,9 @@ namespace libtorrent
TORRENT_ASSERT(m_type == none_t); TORRENT_ASSERT(m_type == none_t);
m_type = int_t; m_type = int_t;
m_data.start = start; m_data.start = start;
m_size = length; m_size = std::uint32_t(length);
m_begin = start - 1; // include 'i' 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 // 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);
TORRENT_ASSERT(end - m_begin < (std::numeric_limits<int>::max)()); TORRENT_ASSERT(end - m_begin < (std::numeric_limits<int>::max)());
m_len = int(end - m_begin); m_len = std::uint32_t(end - m_begin);
} }
// internal // internal

View File

@ -106,8 +106,8 @@ namespace libtorrent
void restrict_mtu(int mtu) void restrict_mtu(int mtu)
{ {
m_restrict_mtu[m_mtu_idx] = mtu; m_restrict_mtu[std::size_t(m_mtu_idx)] = mtu;
m_mtu_idx = (m_mtu_idx + 1) % m_restrict_mtu.size(); m_mtu_idx = (m_mtu_idx + 1) % int(m_restrict_mtu.size());
} }
int restrict_mtu() const int restrict_mtu() const

View File

@ -140,8 +140,8 @@ namespace libtorrent
#define mprotect(buf, size, prot) VirtualProtect(buf, size, prot, nullptr) #define mprotect(buf, size, prot) VirtualProtect(buf, size, prot, nullptr)
#define PROT_READ PAGE_READONLY #define PROT_READ PAGE_READONLY
#endif #endif
mprotect(ret, page, PROT_READ); mprotect(ret, std::size_t(page), PROT_READ);
mprotect(static_cast<char*>(ret) + (num_pages-1) * page, page, PROT_READ); mprotect(static_cast<char*>(ret) + (num_pages-1) * page, std::size_t(page), PROT_READ);
#ifdef TORRENT_WINDOWS #ifdef TORRENT_WINDOWS
#undef mprotect #undef mprotect
@ -168,11 +168,11 @@ namespace libtorrent
#endif #endif
int const page = page_size(); int const page = page_size();
// make the two surrounding pages non-readable and -writable // 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<alloc_header*>(block - page); alloc_header* h = reinterpret_cast<alloc_header*>(block - page);
int const num_pages = int((h->size + (page - 1)) / page + 2); int const num_pages = int((h->size + (page - 1)) / page + 2);
TORRENT_ASSERT(h->magic == 0x1337); 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)); // std::fprintf(stderr, "free: %p head: %p tail: %p size: %d\n", block, block - page, block + h->size, int(h->size));
h->magic = 0; h->magic = 0;
block -= page; block -= page;

View File

@ -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) 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; out += ret;
len -= ret; len -= ret;
if (i - 1 == max_depth && max_depth > 0) break; if (i - 1 == max_depth && max_depth > 0) break;

View File

@ -90,7 +90,7 @@ namespace libtorrent
TORRENT_ASSERT(!m_destructed); TORRENT_ASSERT(!m_destructed);
char* const insert = allocate_appendix(s); char* const insert = allocate_appendix(s);
if (insert == nullptr) return nullptr; if (insert == nullptr) return nullptr;
std::memcpy(insert, buf, s); std::memcpy(insert, buf, std::size_t(s));
return insert; return insert;
} }
@ -137,11 +137,11 @@ namespace libtorrent
if (i->used_size > bytes) if (i->used_size > bytes)
{ {
TORRENT_ASSERT(bytes > 0); TORRENT_ASSERT(bytes > 0);
vec.push_back(Buffer(i->buf, bytes)); vec.push_back(Buffer(i->buf, std::size_t(bytes)));
break; break;
} }
TORRENT_ASSERT(i->used_size > 0); 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; bytes -= i->used_size;
} }
} }

View File

@ -142,7 +142,7 @@ namespace libtorrent
#endif #endif
boost::crc_optimal<32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true> crc; 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(); return crc.checksum();
} }
} }

View File

@ -519,8 +519,8 @@ namespace libtorrent
} }
else if (peers_ent && peers_ent.type() == bdecode_node::list_t) else if (peers_ent && peers_ent.type() == bdecode_node::list_t)
{ {
int len = peers_ent.list_size(); int const len = peers_ent.list_size();
resp.peers.reserve(len); resp.peers.reserve(std::size_t(len));
error_code parse_error; error_code parse_error;
for (int i = 0; i < len; ++i) for (int i = 0; i < len; ++i)
{ {