Skip to content

Commit

Permalink
Adding fixes for signedness warnings in libsolutil
Browse files Browse the repository at this point in the history
  • Loading branch information
mijovic committed Jun 2, 2020
1 parent e7f97cf commit b4454c4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 30 deletions.
9 changes: 6 additions & 3 deletions libsolutil/CommonData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t>(c) >> 4ul) & 0xfu];
ret[i++] = chars[c & 0xfu];
}
assertThrow(i == ret.size(), Exception, "");

Expand All @@ -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<uint8_t> ret;
ret.reserve((_s.size() - s + 1) / 2);
Expand Down
8 changes: 4 additions & 4 deletions libsolutil/CommonData.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
template <class T, class U> std::vector<T>& operator+=(std::vector<T>& _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.
Expand Down Expand Up @@ -299,7 +299,7 @@ template <class T>
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
int i = 0;
unsigned i = 0;
for (T v = _val; v; ++i, v >>= 8) {}
bytes ret(std::max<unsigned>(_min, i), 0);
toBigEndian(_val, ret);
Expand Down Expand Up @@ -379,7 +379,7 @@ void iterateReplacing(std::vector<T>& _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);
Expand All @@ -406,7 +406,7 @@ void iterateReplacingWindow(std::vector<T>& _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);
Expand Down
6 changes: 3 additions & 3 deletions libsolutil/CommonIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t>(length) + c_elementSize - 1) / c_elementSize);
is.read(const_cast<char*>(reinterpret_cast<char const*>(ret.data())), length);
return ret;
}
Expand Down Expand Up @@ -107,8 +107,8 @@ class DisableConsoleBuffering
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);
Expand Down
2 changes: 1 addition & 1 deletion libsolutil/IpfsHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ string base58Encode(bytes const& _data)
string output;
while (data)
{
output += alphabet[size_t(data % alphabet.size())];
output += alphabet[static_cast<size_t>(data % alphabet.size())];
data /= alphabet.size();
}
reverse(output.begin(), output.end());
Expand Down
8 changes: 4 additions & 4 deletions libsolutil/StringUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) +
"...{+" +
Expand Down
22 changes: 11 additions & 11 deletions libsolutil/picosha2.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<word_t>(mask_8bit(*(first + i * 4))) << 24) |
(static_cast<word_t>(mask_8bit(*(first + i * 4 + 1))) << 16) |
(static_cast<word_t>(mask_8bit(*(first + i * 4 + 2))) << 8) |
(static_cast<word_t>(mask_8bit(*(first + i * 4 + 3))));
w[i] = (static_cast<word_t>(mask_8bit(*(first + long(i) * 4))) << 24) |
(static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 1))) << 16) |
(static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 2))) << 8) |
(static_cast<word_t>(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]) +
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -297,20 +297,20 @@ void hash256_impl(RaIter first, RaIter last, OutIter first2, OutIter last2, int,

template <typename InputIter, typename OutIter>
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);
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);
Expand Down
8 changes: 4 additions & 4 deletions test/libsolutil/IpfsHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b4454c4

Please sign in to comment.