From b4454c49255e0276738f8512684c01682bb92110 Mon Sep 17 00:00:00 2001 From: Djordje Mijovic Date: Tue, 2 Jun 2020 15:40:27 +0200 Subject: [PATCH] Adding fixes for signedness warnings in libsolutil --- libsolutil/CommonData.cpp | 9 ++++++--- libsolutil/CommonData.h | 8 ++++---- libsolutil/CommonIO.cpp | 6 +++--- libsolutil/IpfsHash.cpp | 2 +- libsolutil/StringUtils.h | 8 ++++---- libsolutil/picosha2.h | 22 +++++++++++----------- test/libsolutil/IpfsHash.cpp | 8 ++++---- 7 files changed, 33 insertions(+), 30 deletions(-) diff --git a/libsolutil/CommonData.cpp b/libsolutil/CommonData.cpp index bb1a7696c..72071acde 100644 --- a/libsolutil/CommonData.cpp +++ b/libsolutil/CommonData.cpp @@ -64,15 +64,15 @@ string solidity::util::toHex(bytes const& _data, HexPrefix _prefix, HexCase _cas // Mixed case will be handled inside the loop. char const* chars = _case == HexCase::Upper ? upperHexChars : lowerHexChars; - int rix = _data.size() - 1; + size_t rix = _data.size() - 1; for (uint8_t c: _data) { // switch hex case every four hexchars if (_case == HexCase::Mixed) chars = (rix-- & 2) == 0 ? lowerHexChars : upperHexChars; - ret[i++] = chars[(unsigned(c) / 16) & 0xf]; - ret[i++] = chars[unsigned(c) & 0xf]; + ret[i++] = chars[(static_cast(c) >> 4ul) & 0xfu]; + ret[i++] = chars[c & 0xfu]; } 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) { + if (_s.empty()) + return {}; + unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0; std::vector ret; ret.reserve((_s.size() - s + 1) / 2); diff --git a/libsolutil/CommonData.h b/libsolutil/CommonData.h index adac1fb05..a5c045646 100644 --- a/libsolutil/CommonData.h +++ b/libsolutil/CommonData.h @@ -42,7 +42,7 @@ template std::vector& operator+=(std::vector& _a, U& _b) { for (auto const& i: _b) - _a.push_back(i); + _a.push_back(T(i)); return _a; } /// Concatenate the contents of a container onto a vector, move variant. @@ -299,7 +299,7 @@ template inline bytes toCompactBigEndian(T _val, unsigned _min = 0) { static_assert(std::is_same::value || !std::numeric_limits::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) {} bytes ret(std::max(_min, i), 0); toBigEndian(_val, ret); @@ -379,7 +379,7 @@ void iterateReplacing(std::vector& _vector, F const& _f) { 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; } modifiedVector += std::move(*r); @@ -406,7 +406,7 @@ void iterateReplacingWindow(std::vector& _vector, F const& _f, std::index_seq { 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; } modifiedVector += std::move(*r); diff --git a/libsolutil/CommonIO.cpp b/libsolutil/CommonIO.cpp index ec6c33538..cbe68f0d1 100644 --- a/libsolutil/CommonIO.cpp +++ b/libsolutil/CommonIO.cpp @@ -56,7 +56,7 @@ inline T readFile(std::string const& _file) return ret; // do not read empty file (MSVC does not like it) is.seekg(0, is.beg); - ret.resize((length + c_elementSize - 1) / c_elementSize); + ret.resize((static_cast(length) + c_elementSize - 1) / c_elementSize); is.read(const_cast(reinterpret_cast(ret.data())), length); return ret; } @@ -107,8 +107,8 @@ public: DisableConsoleBuffering() { tcgetattr(0, &m_termios); - m_termios.c_lflag &= ~ICANON; - m_termios.c_lflag &= ~ECHO; + m_termios.c_lflag &= ~tcflag_t(ICANON); + m_termios.c_lflag &= ~tcflag_t(ECHO); m_termios.c_cc[VMIN] = 1; m_termios.c_cc[VTIME] = 0; tcsetattr(0, TCSANOW, &m_termios); diff --git a/libsolutil/IpfsHash.cpp b/libsolutil/IpfsHash.cpp index 1f9435748..6913d42f0 100644 --- a/libsolutil/IpfsHash.cpp +++ b/libsolutil/IpfsHash.cpp @@ -62,7 +62,7 @@ string base58Encode(bytes const& _data) string output; while (data) { - output += alphabet[size_t(data % alphabet.size())]; + output += alphabet[static_cast(data % alphabet.size())]; data /= alphabet.size(); } reverse(output.begin(), output.end()); diff --git a/libsolutil/StringUtils.h b/libsolutil/StringUtils.h index 98158aa6d..caea637a2 100644 --- a/libsolutil/StringUtils.h +++ b/libsolutil/StringUtils.h @@ -156,14 +156,14 @@ inline std::string formatNumberReadable( if (_useTruncation) { // return as interior-truncated hex. - int len = str.size(); + size_t len = str.size(); if (len < 24) return str; - int const initialChars = (prefix == HexPrefix::Add) ? 6 : 4; - int const finalChars = 4; - int numSkipped = len - initialChars - finalChars; + size_t const initialChars = (prefix == HexPrefix::Add) ? 6 : 4; + size_t const finalChars = 4; + size_t numSkipped = len - initialChars - finalChars; return str.substr(0, initialChars) + "...{+" + diff --git a/libsolutil/picosha2.h b/libsolutil/picosha2.h index fdfabb5ac..6994455a2 100644 --- a/libsolutil/picosha2.h +++ b/libsolutil/picosha2.h @@ -95,10 +95,10 @@ void hash256_block(RaIter1 message_digest, RaIter2 first, RaIter2 last) { word_t w[64]; std::fill(w, w + 64, 0); for (std::size_t i = 0; i < 16; ++i) { - w[i] = (static_cast(mask_8bit(*(first + i * 4))) << 24) | - (static_cast(mask_8bit(*(first + i * 4 + 1))) << 16) | - (static_cast(mask_8bit(*(first + i * 4 + 2))) << 8) | - (static_cast(mask_8bit(*(first + i * 4 + 3)))); + w[i] = (static_cast(mask_8bit(*(first + long(i) * 4))) << 24) | + (static_cast(mask_8bit(*(first + long(i) * 4 + 1))) << 16) | + (static_cast(mask_8bit(*(first + long(i) * 4 + 2))) << 8) | + (static_cast(mask_8bit(*(first + long(i) * 4 + 3)))); } for (std::size_t i = 16; i < 64; ++i) { 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::size_t i = 0; for (; i + 64 <= buffer_.size(); i += 64) { - detail::hash256_block(h_, buffer_.begin() + i, - buffer_.begin() + i + 64); + detail::hash256_block(h_, buffer_.begin() + long(i), + buffer_.begin() + long(i) + 64); } - buffer_.erase(buffer_.begin(), buffer_.begin() + i); + buffer_.erase(buffer_.begin(), buffer_.begin() + long(i)); } void finish() { @@ -297,20 +297,20 @@ void hash256_impl(RaIter first, RaIter last, OutIter first2, OutIter last2, int, template 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 buffer(buffer_size); hash256_one_by_one hasher; // hasher.init(); while (first != last) { - int size = buffer_size; - for (int i = 0; i != buffer_size; ++i, ++first) { + size_t size = buffer_size; + for (size_t i = 0; i != buffer_size; ++i, ++first) { if (first == last) { size = i; break; } buffer[i] = *first; } - hasher.process(buffer.begin(), buffer.begin() + size); + hasher.process(buffer.begin(), buffer.begin() + ptrdiff_t(size)); } hasher.finish(); hasher.get_hash_bytes(first2, last2); diff --git a/test/libsolutil/IpfsHash.cpp b/test/libsolutil/IpfsHash.cpp index 9c4fb5633..9d6f0f8fc 100644 --- a/test/libsolutil/IpfsHash.cpp +++ b/test/libsolutil/IpfsHash.cpp @@ -36,10 +36,10 @@ BOOST_AUTO_TEST_CASE(test_small) BOOST_CHECK_EQUAL(ipfsHashBase58({}), "QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH"); BOOST_CHECK_EQUAL(ipfsHashBase58("x"), "QmULKig5Fxrs2sC4qt9nNduucXfb92AFYQ6Hi3YRqDmrYC"); BOOST_CHECK_EQUAL(ipfsHashBase58("Solidity\n"), "QmSsm9M7PQRBnyiz1smizk8hZw3URfk8fSeHzeTo3oZidS"); - BOOST_CHECK_EQUAL(ipfsHashBase58(string(size_t(200), char(0))), "QmSXR1N23uWzsANi8wpxMPw5dmmhqBVUAb4hUrHVLpNaMr"); - BOOST_CHECK_EQUAL(ipfsHashBase58(string(size_t(10250), char(0))), "QmVJJBB3gKKBWYC9QTywpH8ZL1bDeTDJ17B63Af5kino9i"); - BOOST_CHECK_EQUAL(ipfsHashBase58(string(size_t(100000), char(0))), "QmYgKa25YqEGpQmmZtPPFMNK3kpqqneHk6nMSEUYryEX1C"); - BOOST_CHECK_EQUAL(ipfsHashBase58(string(size_t(121071), char(0))), "QmdMdRshQmqvyc92N82r7AKYdUF5FRh4DJo6GtrmEk3wgj"); + BOOST_CHECK_EQUAL(ipfsHashBase58(string(200ul, char(0))), "QmSXR1N23uWzsANi8wpxMPw5dmmhqBVUAb4hUrHVLpNaMr"); + BOOST_CHECK_EQUAL(ipfsHashBase58(string(10250ul, char(0))), "QmVJJBB3gKKBWYC9QTywpH8ZL1bDeTDJ17B63Af5kino9i"); + BOOST_CHECK_EQUAL(ipfsHashBase58(string(100000ul, char(0))), "QmYgKa25YqEGpQmmZtPPFMNK3kpqqneHk6nMSEUYryEX1C"); + BOOST_CHECK_EQUAL(ipfsHashBase58(string(121071ul, char(0))), "QmdMdRshQmqvyc92N82r7AKYdUF5FRh4DJo6GtrmEk3wgj"); } BOOST_AUTO_TEST_CASE(test_medium)