Skip to content

Commit b4454c4

Browse files
committed
Adding fixes for signedness warnings in libsolutil
1 parent e7f97cf commit b4454c4

File tree

7 files changed

+33
-30
lines changed

7 files changed

+33
-30
lines changed

libsolutil/CommonData.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ string solidity::util::toHex(bytes const& _data, HexPrefix _prefix, HexCase _cas
6464

6565
// Mixed case will be handled inside the loop.
6666
char const* chars = _case == HexCase::Upper ? upperHexChars : lowerHexChars;
67-
int rix = _data.size() - 1;
67+
size_t rix = _data.size() - 1;
6868
for (uint8_t c: _data)
6969
{
7070
// switch hex case every four hexchars
7171
if (_case == HexCase::Mixed)
7272
chars = (rix-- & 2) == 0 ? lowerHexChars : upperHexChars;
7373

74-
ret[i++] = chars[(unsigned(c) / 16) & 0xf];
75-
ret[i++] = chars[unsigned(c) & 0xf];
74+
ret[i++] = chars[(static_cast<size_t>(c) >> 4ul) & 0xfu];
75+
ret[i++] = chars[c & 0xfu];
7676
}
7777
assertThrow(i == ret.size(), Exception, "");
7878

@@ -95,6 +95,9 @@ int solidity::util::fromHex(char _i, WhenError _throw)
9595

9696
bytes solidity::util::fromHex(std::string const& _s, WhenError _throw)
9797
{
98+
if (_s.empty())
99+
return {};
100+
98101
unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0;
99102
std::vector<uint8_t> ret;
100103
ret.reserve((_s.size() - s + 1) / 2);

libsolutil/CommonData.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
template <class T, class U> std::vector<T>& operator+=(std::vector<T>& _a, U& _b)
4343
{
4444
for (auto const& i: _b)
45-
_a.push_back(i);
45+
_a.push_back(T(i));
4646
return _a;
4747
}
4848
/// Concatenate the contents of a container onto a vector, move variant.
@@ -299,7 +299,7 @@ template <class T>
299299
inline bytes toCompactBigEndian(T _val, unsigned _min = 0)
300300
{
301301
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
302-
int i = 0;
302+
unsigned i = 0;
303303
for (T v = _val; v; ++i, v >>= 8) {}
304304
bytes ret(std::max<unsigned>(_min, i), 0);
305305
toBigEndian(_val, ret);
@@ -379,7 +379,7 @@ void iterateReplacing(std::vector<T>& _vector, F const& _f)
379379
{
380380
if (!useModified)
381381
{
382-
std::move(_vector.begin(), _vector.begin() + i, back_inserter(modifiedVector));
382+
std::move(_vector.begin(), _vector.begin() + ptrdiff_t(i), back_inserter(modifiedVector));
383383
useModified = true;
384384
}
385385
modifiedVector += std::move(*r);
@@ -406,7 +406,7 @@ void iterateReplacingWindow(std::vector<T>& _vector, F const& _f, std::index_seq
406406
{
407407
if (!useModified)
408408
{
409-
std::move(_vector.begin(), _vector.begin() + i, back_inserter(modifiedVector));
409+
std::move(_vector.begin(), _vector.begin() + ptrdiff_t(i), back_inserter(modifiedVector));
410410
useModified = true;
411411
}
412412
modifiedVector += std::move(*r);

libsolutil/CommonIO.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ inline T readFile(std::string const& _file)
5656
return ret; // do not read empty file (MSVC does not like it)
5757
is.seekg(0, is.beg);
5858

59-
ret.resize((length + c_elementSize - 1) / c_elementSize);
59+
ret.resize((static_cast<size_t>(length) + c_elementSize - 1) / c_elementSize);
6060
is.read(const_cast<char*>(reinterpret_cast<char const*>(ret.data())), length);
6161
return ret;
6262
}
@@ -107,8 +107,8 @@ class DisableConsoleBuffering
107107
DisableConsoleBuffering()
108108
{
109109
tcgetattr(0, &m_termios);
110-
m_termios.c_lflag &= ~ICANON;
111-
m_termios.c_lflag &= ~ECHO;
110+
m_termios.c_lflag &= ~tcflag_t(ICANON);
111+
m_termios.c_lflag &= ~tcflag_t(ECHO);
112112
m_termios.c_cc[VMIN] = 1;
113113
m_termios.c_cc[VTIME] = 0;
114114
tcsetattr(0, TCSANOW, &m_termios);

libsolutil/IpfsHash.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ string base58Encode(bytes const& _data)
6262
string output;
6363
while (data)
6464
{
65-
output += alphabet[size_t(data % alphabet.size())];
65+
output += alphabet[static_cast<size_t>(data % alphabet.size())];
6666
data /= alphabet.size();
6767
}
6868
reverse(output.begin(), output.end());

libsolutil/StringUtils.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,14 @@ inline std::string formatNumberReadable(
156156
if (_useTruncation)
157157
{
158158
// return as interior-truncated hex.
159-
int len = str.size();
159+
size_t len = str.size();
160160

161161
if (len < 24)
162162
return str;
163163

164-
int const initialChars = (prefix == HexPrefix::Add) ? 6 : 4;
165-
int const finalChars = 4;
166-
int numSkipped = len - initialChars - finalChars;
164+
size_t const initialChars = (prefix == HexPrefix::Add) ? 6 : 4;
165+
size_t const finalChars = 4;
166+
size_t numSkipped = len - initialChars - finalChars;
167167

168168
return str.substr(0, initialChars) +
169169
"...{+" +

libsolutil/picosha2.h

+11-11
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@ void hash256_block(RaIter1 message_digest, RaIter2 first, RaIter2 last) {
9595
word_t w[64];
9696
std::fill(w, w + 64, 0);
9797
for (std::size_t i = 0; i < 16; ++i) {
98-
w[i] = (static_cast<word_t>(mask_8bit(*(first + i * 4))) << 24) |
99-
(static_cast<word_t>(mask_8bit(*(first + i * 4 + 1))) << 16) |
100-
(static_cast<word_t>(mask_8bit(*(first + i * 4 + 2))) << 8) |
101-
(static_cast<word_t>(mask_8bit(*(first + i * 4 + 3))));
98+
w[i] = (static_cast<word_t>(mask_8bit(*(first + long(i) * 4))) << 24) |
99+
(static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 1))) << 16) |
100+
(static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 2))) << 8) |
101+
(static_cast<word_t>(mask_8bit(*(first + long(i) * 4 + 3))));
102102
}
103103
for (std::size_t i = 16; i < 64; ++i) {
104104
w[i] = mask_32bit(ssig1(w[i - 2]) + w[i - 7] + ssig0(w[i - 15]) +
@@ -196,10 +196,10 @@ class hash256_one_by_one {
196196
std::copy(first, last, std::back_inserter(buffer_));
197197
std::size_t i = 0;
198198
for (; i + 64 <= buffer_.size(); i += 64) {
199-
detail::hash256_block(h_, buffer_.begin() + i,
200-
buffer_.begin() + i + 64);
199+
detail::hash256_block(h_, buffer_.begin() + long(i),
200+
buffer_.begin() + long(i) + 64);
201201
}
202-
buffer_.erase(buffer_.begin(), buffer_.begin() + i);
202+
buffer_.erase(buffer_.begin(), buffer_.begin() + long(i));
203203
}
204204

205205
void finish() {
@@ -297,20 +297,20 @@ void hash256_impl(RaIter first, RaIter last, OutIter first2, OutIter last2, int,
297297

298298
template <typename InputIter, typename OutIter>
299299
void hash256_impl(InputIter first, InputIter last, OutIter first2,
300-
OutIter last2, int buffer_size, std::input_iterator_tag) {
300+
OutIter last2, size_t buffer_size, std::input_iterator_tag) {
301301
std::vector<byte_t> buffer(buffer_size);
302302
hash256_one_by_one hasher;
303303
// hasher.init();
304304
while (first != last) {
305-
int size = buffer_size;
306-
for (int i = 0; i != buffer_size; ++i, ++first) {
305+
size_t size = buffer_size;
306+
for (size_t i = 0; i != buffer_size; ++i, ++first) {
307307
if (first == last) {
308308
size = i;
309309
break;
310310
}
311311
buffer[i] = *first;
312312
}
313-
hasher.process(buffer.begin(), buffer.begin() + size);
313+
hasher.process(buffer.begin(), buffer.begin() + ptrdiff_t(size));
314314
}
315315
hasher.finish();
316316
hasher.get_hash_bytes(first2, last2);

test/libsolutil/IpfsHash.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ BOOST_AUTO_TEST_CASE(test_small)
3636
BOOST_CHECK_EQUAL(ipfsHashBase58({}), "QmbFMke1KXqnYyBBWxB74N4c5SBnJMVAiMNRcGu6x1AwQH");
3737
BOOST_CHECK_EQUAL(ipfsHashBase58("x"), "QmULKig5Fxrs2sC4qt9nNduucXfb92AFYQ6Hi3YRqDmrYC");
3838
BOOST_CHECK_EQUAL(ipfsHashBase58("Solidity\n"), "QmSsm9M7PQRBnyiz1smizk8hZw3URfk8fSeHzeTo3oZidS");
39-
BOOST_CHECK_EQUAL(ipfsHashBase58(string(size_t(200), char(0))), "QmSXR1N23uWzsANi8wpxMPw5dmmhqBVUAb4hUrHVLpNaMr");
40-
BOOST_CHECK_EQUAL(ipfsHashBase58(string(size_t(10250), char(0))), "QmVJJBB3gKKBWYC9QTywpH8ZL1bDeTDJ17B63Af5kino9i");
41-
BOOST_CHECK_EQUAL(ipfsHashBase58(string(size_t(100000), char(0))), "QmYgKa25YqEGpQmmZtPPFMNK3kpqqneHk6nMSEUYryEX1C");
42-
BOOST_CHECK_EQUAL(ipfsHashBase58(string(size_t(121071), char(0))), "QmdMdRshQmqvyc92N82r7AKYdUF5FRh4DJo6GtrmEk3wgj");
39+
BOOST_CHECK_EQUAL(ipfsHashBase58(string(200ul, char(0))), "QmSXR1N23uWzsANi8wpxMPw5dmmhqBVUAb4hUrHVLpNaMr");
40+
BOOST_CHECK_EQUAL(ipfsHashBase58(string(10250ul, char(0))), "QmVJJBB3gKKBWYC9QTywpH8ZL1bDeTDJ17B63Af5kino9i");
41+
BOOST_CHECK_EQUAL(ipfsHashBase58(string(100000ul, char(0))), "QmYgKa25YqEGpQmmZtPPFMNK3kpqqneHk6nMSEUYryEX1C");
42+
BOOST_CHECK_EQUAL(ipfsHashBase58(string(121071ul, char(0))), "QmdMdRshQmqvyc92N82r7AKYdUF5FRh4DJo6GtrmEk3wgj");
4343
}
4444

4545
BOOST_AUTO_TEST_CASE(test_medium)

0 commit comments

Comments
 (0)