Adding fixes for signedness warnings in libsolutil

This commit is contained in:
Djordje Mijovic 2020-06-02 15:40:27 +02:00
parent e7f97cf3ac
commit b4454c4925
7 changed files with 33 additions and 30 deletions

View File

@ -64,15 +64,15 @@ string solidity::util::toHex(bytes const& _data, HexPrefix _prefix, HexCase _cas
// Mixed case will be handled inside the loop. // Mixed case will be handled inside the loop.
char const* chars = _case == HexCase::Upper ? upperHexChars : lowerHexChars; char const* chars = _case == HexCase::Upper ? upperHexChars : lowerHexChars;
int rix = _data.size() - 1; size_t rix = _data.size() - 1;
for (uint8_t c: _data) for (uint8_t c: _data)
{ {
// switch hex case every four hexchars // switch hex case every four hexchars
if (_case == HexCase::Mixed) if (_case == HexCase::Mixed)
chars = (rix-- & 2) == 0 ? lowerHexChars : upperHexChars; chars = (rix-- & 2) == 0 ? lowerHexChars : upperHexChars;
ret[i++] = chars[(unsigned(c) / 16) & 0xf]; ret[i++] = chars[(static_cast<size_t>(c) >> 4ul) & 0xfu];
ret[i++] = chars[unsigned(c) & 0xf]; ret[i++] = chars[c & 0xfu];
} }
assertThrow(i == ret.size(), Exception, ""); assertThrow(i == ret.size(), Exception, "");
@ -95,6 +95,9 @@ int solidity::util::fromHex(char _i, WhenError _throw)
bytes solidity::util::fromHex(std::string const& _s, WhenError _throw) bytes solidity::util::fromHex(std::string const& _s, WhenError _throw)
{ {
if (_s.empty())
return {};
unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0; unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0;
std::vector<uint8_t> ret; std::vector<uint8_t> ret;
ret.reserve((_s.size() - s + 1) / 2); ret.reserve((_s.size() - s + 1) / 2);

View File

@ -42,7 +42,7 @@
template <class T, class U> std::vector<T>& operator+=(std::vector<T>& _a, U& _b) template <class T, class U> std::vector<T>& operator+=(std::vector<T>& _a, U& _b)
{ {
for (auto const& i: _b) for (auto const& i: _b)
_a.push_back(i); _a.push_back(T(i));
return _a; return _a;
} }
/// Concatenate the contents of a container onto a vector, move variant. /// Concatenate the contents of a container onto a vector, move variant.
@ -299,7 +299,7 @@ template <class T>
inline bytes toCompactBigEndian(T _val, unsigned _min = 0) inline bytes toCompactBigEndian(T _val, unsigned _min = 0)
{ {
static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift static_assert(std::is_same<bigint, T>::value || !std::numeric_limits<T>::is_signed, "only unsigned types or bigint supported"); //bigint does not carry sign bit on shift
int i = 0; unsigned i = 0;
for (T v = _val; v; ++i, v >>= 8) {} for (T v = _val; v; ++i, v >>= 8) {}
bytes ret(std::max<unsigned>(_min, i), 0); bytes ret(std::max<unsigned>(_min, i), 0);
toBigEndian(_val, ret); toBigEndian(_val, ret);
@ -379,7 +379,7 @@ void iterateReplacing(std::vector<T>& _vector, F const& _f)
{ {
if (!useModified) if (!useModified)
{ {
std::move(_vector.begin(), _vector.begin() + i, back_inserter(modifiedVector)); std::move(_vector.begin(), _vector.begin() + ptrdiff_t(i), back_inserter(modifiedVector));
useModified = true; useModified = true;
} }
modifiedVector += std::move(*r); modifiedVector += std::move(*r);
@ -406,7 +406,7 @@ void iterateReplacingWindow(std::vector<T>& _vector, F const& _f, std::index_seq
{ {
if (!useModified) if (!useModified)
{ {
std::move(_vector.begin(), _vector.begin() + i, back_inserter(modifiedVector)); std::move(_vector.begin(), _vector.begin() + ptrdiff_t(i), back_inserter(modifiedVector));
useModified = true; useModified = true;
} }
modifiedVector += std::move(*r); modifiedVector += std::move(*r);

View File

@ -56,7 +56,7 @@ inline T readFile(std::string const& _file)
return ret; // do not read empty file (MSVC does not like it) return ret; // do not read empty file (MSVC does not like it)
is.seekg(0, is.beg); is.seekg(0, is.beg);
ret.resize((length + c_elementSize - 1) / c_elementSize); ret.resize((static_cast<size_t>(length) + c_elementSize - 1) / c_elementSize);
is.read(const_cast<char*>(reinterpret_cast<char const*>(ret.data())), length); is.read(const_cast<char*>(reinterpret_cast<char const*>(ret.data())), length);
return ret; return ret;
} }
@ -107,8 +107,8 @@ public:
DisableConsoleBuffering() DisableConsoleBuffering()
{ {
tcgetattr(0, &m_termios); tcgetattr(0, &m_termios);
m_termios.c_lflag &= ~ICANON; m_termios.c_lflag &= ~tcflag_t(ICANON);
m_termios.c_lflag &= ~ECHO; m_termios.c_lflag &= ~tcflag_t(ECHO);
m_termios.c_cc[VMIN] = 1; m_termios.c_cc[VMIN] = 1;
m_termios.c_cc[VTIME] = 0; m_termios.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &m_termios); tcsetattr(0, TCSANOW, &m_termios);

View File

@ -62,7 +62,7 @@ string base58Encode(bytes const& _data)
string output; string output;
while (data) while (data)
{ {
output += alphabet[size_t(data % alphabet.size())]; output += alphabet[static_cast<size_t>(data % alphabet.size())];
data /= alphabet.size(); data /= alphabet.size();
} }
reverse(output.begin(), output.end()); reverse(output.begin(), output.end());

View File

@ -156,14 +156,14 @@ inline std::string formatNumberReadable(
if (_useTruncation) if (_useTruncation)
{ {
// return as interior-truncated hex. // return as interior-truncated hex.
int len = str.size(); size_t len = str.size();
if (len < 24) if (len < 24)
return str; return str;
int const initialChars = (prefix == HexPrefix::Add) ? 6 : 4; size_t const initialChars = (prefix == HexPrefix::Add) ? 6 : 4;
int const finalChars = 4; size_t const finalChars = 4;
int numSkipped = len - initialChars - finalChars; size_t numSkipped = len - initialChars - finalChars;
return str.substr(0, initialChars) + return str.substr(0, initialChars) +
"...{+" + "...{+" +

View File

@ -95,10 +95,10 @@ void hash256_block(RaIter1 message_digest, RaIter2 first, RaIter2 last) {
word_t w[64]; word_t w[64];
std::fill(w, w + 64, 0); std::fill(w, w + 64, 0);
for (std::size_t i = 0; i < 16; ++i) { for (std::size_t i = 0; i < 16; ++i) {
w[i] = (static_cast<word_t>(mask_8bit(*(first + i * 4))) << 24) | w[i] = (static_cast<word_t>(mask_8bit(*(first + long(i) * 4))) << 24) |
(static_cast<word_t>(mask_8bit(*(first + i * 4 + 1))) << 16) | (static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 1))) << 16) |
(static_cast<word_t>(mask_8bit(*(first + i * 4 + 2))) << 8) | (static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 2))) << 8) |
(static_cast<word_t>(mask_8bit(*(first + i * 4 + 3)))); (static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 3))));
} }
for (std::size_t i = 16; i < 64; ++i) { for (std::size_t i = 16; i < 64; ++i) {
w[i] = mask_32bit(ssig1(w[i - 2]) + w[i - 7] + ssig0(w[i - 15]) + w[i] = mask_32bit(ssig1(w[i - 2]) + w[i - 7] + ssig0(w[i - 15]) +
@ -196,10 +196,10 @@ class hash256_one_by_one {
std::copy(first, last, std::back_inserter(buffer_)); std::copy(first, last, std::back_inserter(buffer_));
std::size_t i = 0; std::size_t i = 0;
for (; i + 64 <= buffer_.size(); i += 64) { for (; i + 64 <= buffer_.size(); i += 64) {
detail::hash256_block(h_, buffer_.begin() + i, detail::hash256_block(h_, buffer_.begin() + long(i),
buffer_.begin() + i + 64); buffer_.begin() + long(i) + 64);
} }
buffer_.erase(buffer_.begin(), buffer_.begin() + i); buffer_.erase(buffer_.begin(), buffer_.begin() + long(i));
} }
void finish() { void finish() {
@ -297,20 +297,20 @@ void hash256_impl(RaIter first, RaIter last, OutIter first2, OutIter last2, int,
template <typename InputIter, typename OutIter> template <typename InputIter, typename OutIter>
void hash256_impl(InputIter first, InputIter last, OutIter first2, void hash256_impl(InputIter first, InputIter last, OutIter first2,
OutIter last2, int buffer_size, std::input_iterator_tag) { OutIter last2, size_t buffer_size, std::input_iterator_tag) {
std::vector<byte_t> buffer(buffer_size); std::vector<byte_t> buffer(buffer_size);
hash256_one_by_one hasher; hash256_one_by_one hasher;
// hasher.init(); // hasher.init();
while (first != last) { while (first != last) {
int size = buffer_size; size_t size = buffer_size;
for (int i = 0; i != buffer_size; ++i, ++first) { for (size_t i = 0; i != buffer_size; ++i, ++first) {
if (first == last) { if (first == last) {
size = i; size = i;
break; break;
} }
buffer[i] = *first; buffer[i] = *first;
} }
hasher.process(buffer.begin(), buffer.begin() + size); hasher.process(buffer.begin(), buffer.begin() + ptrdiff_t(size));
} }
hasher.finish(); hasher.finish();
hasher.get_hash_bytes(first2, last2); hasher.get_hash_bytes(first2, last2);

View File

@ -36,10 +36,10 @@ BOOST_AUTO_TEST_CASE(test_small)
BOOST_CHECK_EQUAL(ipfsHashBase58({}), "QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"); BOOST_CHECK_EQUAL(ipfsHashBase58({}), "QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH");
BOOST_CHECK_EQUAL(ipfsHashBase58("x"), "QmULKig5Fxrs2sC4qt9nNduucXfb92AFYQ6Hi3YRqDmrYC"); BOOST_CHECK_EQUAL(ipfsHashBase58("x"), "QmULKig5Fxrs2sC4qt9nNduucXfb92AFYQ6Hi3YRqDmrYC");
BOOST_CHECK_EQUAL(ipfsHashBase58("Solidity\n"), "QmSsm9M7PQRBnyiz1smizk8hZw3URfk8fSeHzeTo3oZidS"); BOOST_CHECK_EQUAL(ipfsHashBase58("Solidity\n"), "QmSsm9M7PQRBnyiz1smizk8hZw3URfk8fSeHzeTo3oZidS");
BOOST_CHECK_EQUAL(ipfsHashBase58(string(size_t(200), char(0))), "QmSXR1N23uWzsANi8wpxMPw5dmmhqBVUAb4hUrHVLpNaMr"); BOOST_CHECK_EQUAL(ipfsHashBase58(string(200ul, char(0))), "QmSXR1N23uWzsANi8wpxMPw5dmmhqBVUAb4hUrHVLpNaMr");
BOOST_CHECK_EQUAL(ipfsHashBase58(string(size_t(10250), char(0))), "QmVJJBB3gKKBWYC9QTywpH8ZL1bDeTDJ17B63Af5kino9i"); BOOST_CHECK_EQUAL(ipfsHashBase58(string(10250ul, char(0))), "QmVJJBB3gKKBWYC9QTywpH8ZL1bDeTDJ17B63Af5kino9i");
BOOST_CHECK_EQUAL(ipfsHashBase58(string(size_t(100000), char(0))), "QmYgKa25YqEGpQmmZtPPFMNK3kpqqneHk6nMSEUYryEX1C"); BOOST_CHECK_EQUAL(ipfsHashBase58(string(100000ul, char(0))), "QmYgKa25YqEGpQmmZtPPFMNK3kpqqneHk6nMSEUYryEX1C");
BOOST_CHECK_EQUAL(ipfsHashBase58(string(size_t(121071), char(0))), "QmdMdRshQmqvyc92N82r7AKYdUF5FRh4DJo6GtrmEk3wgj"); BOOST_CHECK_EQUAL(ipfsHashBase58(string(121071ul, char(0))), "QmdMdRshQmqvyc92N82r7AKYdUF5FRh4DJo6GtrmEk3wgj");
} }
BOOST_AUTO_TEST_CASE(test_medium) BOOST_AUTO_TEST_CASE(test_medium)