Skip to content

Commit 7006496

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23756: refactor: Fix implicit integer sign changes in strencodings
fa5865a Reduce size of strencodings decode tables (MarcoFalke) fad6761 Fix implicit integer sign changes in strencodings (MarcoFalke) Pull request description: This removes casts where they are nonsensical (ToUpper/ToLower) and adds them where needed. Then, remove the suppressions. Also, reduce static memory usage. ACKs for top commit: shaavan: crACK fa5865a Tree-SHA512: 29850fb616789befad17f71731f322b2238e94ec431cda293629de540f7ab02060a9fc5b7666168ca2592048df5a39a2975177f47771d4d8211d90321052549d
2 parents 40fdb9e + fa5865a commit 7006496

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

src/util/strencodings.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ std::vector<unsigned char> ParseHex(const char* psz)
9292
signed char c = HexDigit(*psz++);
9393
if (c == (signed char)-1)
9494
break;
95-
unsigned char n = (c << 4);
95+
auto n{uint8_t(c << 4)};
9696
c = HexDigit(*psz++);
9797
if (c == (signed char)-1)
9898
break;
@@ -141,8 +141,7 @@ std::string EncodeBase64(Span<const unsigned char> input)
141141

142142
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
143143
{
144-
static const int decode64_table[256] =
145-
{
144+
static const int8_t decode64_table[256]{
146145
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
147146
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
148147
-1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1,
@@ -164,7 +163,7 @@ std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
164163
while (*p != 0) {
165164
int x = decode64_table[(unsigned char)*p];
166165
if (x == -1) break;
167-
val.push_back(x);
166+
val.push_back(uint8_t(x));
168167
++p;
169168
}
170169

@@ -220,8 +219,7 @@ std::string EncodeBase32(const std::string& str, bool pad)
220219

221220
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)
222221
{
223-
static const int decode32_table[256] =
224-
{
222+
static const int8_t decode32_table[256]{
225223
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
226224
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
227225
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1,
@@ -243,7 +241,7 @@ std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)
243241
while (*p != 0) {
244242
int x = decode32_table[(unsigned char)*p];
245243
if (x == -1) break;
246-
val.push_back(x);
244+
val.push_back(uint8_t(x));
247245
++p;
248246
}
249247

@@ -491,14 +489,14 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
491489
std::string ToLower(const std::string& str)
492490
{
493491
std::string r;
494-
for (auto ch : str) r += ToLower((unsigned char)ch);
492+
for (auto ch : str) r += ToLower(ch);
495493
return r;
496494
}
497495

498496
std::string ToUpper(const std::string& str)
499497
{
500498
std::string r;
501-
for (auto ch : str) r += ToUpper((unsigned char)ch);
499+
for (auto ch : str) r += ToUpper(ch);
502500
return r;
503501
}
504502

src/util/strencodings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ bool TimingResistantEqual(const T& a, const T& b)
226226
if (b.size() == 0) return a.size() == 0;
227227
size_t accumulator = a.size() ^ b.size();
228228
for (size_t i = 0; i < a.size(); i++)
229-
accumulator |= a[i] ^ b[i%b.size()];
229+
accumulator |= size_t(a[i] ^ b[i%b.size()]);
230230
return accumulator == 0;
231231
}
232232

test/sanitizer_suppressions/ubsan

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ implicit-integer-sign-change:test/skiplist_tests.cpp
7676
implicit-integer-sign-change:test/streams_tests.cpp
7777
implicit-integer-sign-change:test/transaction_tests.cpp
7878
implicit-integer-sign-change:txmempool.cpp
79-
implicit-integer-sign-change:util/strencodings.cpp
80-
implicit-integer-sign-change:util/strencodings.h
8179
implicit-integer-sign-change:validation.cpp
8280
implicit-integer-sign-change:zmq/zmqpublishnotifier.cpp
8381
implicit-signed-integer-truncation,implicit-integer-sign-change:chain.h

0 commit comments

Comments
 (0)