Skip to content

Commit 2bcf1fc

Browse files
committed
Pass a maximum output length to DecodeBase58 and DecodeBase58Check
Also remove a needless loop in DecodeBase58 to prune zeroes in the base256 output of the conversion. The number of zeroes is implied by keeping track explicitly of the length during the loop.
1 parent b4a1da9 commit 2bcf1fc

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

src/base58.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <assert.h>
1212
#include <string.h>
1313

14+
#include <limits>
15+
1416
/** All alphanumeric characters except for "0", "I", "O", and "l" */
1517
static const char* pszBase58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
1618
static const int8_t mapBase58[256] = {
@@ -32,7 +34,7 @@ static const int8_t mapBase58[256] = {
3234
-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,
3335
};
3436

35-
bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
37+
bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch, int max_ret_len)
3638
{
3739
// Skip leading spaces.
3840
while (*psz && IsSpace(*psz))
@@ -42,6 +44,7 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
4244
int length = 0;
4345
while (*psz == '1') {
4446
zeroes++;
47+
if (zeroes > max_ret_len) return false;
4548
psz++;
4649
}
4750
// Allocate enough space in big-endian base256 representation.
@@ -62,6 +65,7 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
6265
}
6366
assert(carry == 0);
6467
length = i;
68+
if (length + zeroes > max_ret_len) return false;
6569
psz++;
6670
}
6771
// Skip trailing spaces.
@@ -71,8 +75,6 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch)
7175
return false;
7276
// Skip leading zeroes in b256.
7377
std::vector<unsigned char>::iterator it = b256.begin() + (size - length);
74-
while (it != b256.end() && *it == 0)
75-
it++;
7678
// Copy result into output vector.
7779
vch.reserve(zeroes + (b256.end() - it));
7880
vch.assign(zeroes, 0x00);
@@ -126,9 +128,9 @@ std::string EncodeBase58(const std::vector<unsigned char>& vch)
126128
return EncodeBase58(vch.data(), vch.data() + vch.size());
127129
}
128130

129-
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet)
131+
bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len)
130132
{
131-
return DecodeBase58(str.c_str(), vchRet);
133+
return DecodeBase58(str.c_str(), vchRet, max_ret_len);
132134
}
133135

134136
std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
@@ -140,9 +142,9 @@ std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn)
140142
return EncodeBase58(vch);
141143
}
142144

143-
bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
145+
bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet, int max_ret_len)
144146
{
145-
if (!DecodeBase58(psz, vchRet) ||
147+
if (!DecodeBase58(psz, vchRet, max_ret_len > std::numeric_limits<int>::max() - 4 ? std::numeric_limits<int>::max() : max_ret_len + 4) ||
146148
(vchRet.size() < 4)) {
147149
vchRet.clear();
148150
return false;
@@ -157,7 +159,7 @@ bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet)
157159
return true;
158160
}
159161

160-
bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet)
162+
bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret)
161163
{
162-
return DecodeBase58Check(str.c_str(), vchRet);
164+
return DecodeBase58Check(str.c_str(), vchRet, max_ret);
163165
}

src/base58.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <attributes.h>
1818

19+
#include <limits>
1920
#include <string>
2021
#include <vector>
2122

@@ -35,13 +36,13 @@ std::string EncodeBase58(const std::vector<unsigned char>& vch);
3536
* return true if decoding is successful.
3637
* psz cannot be nullptr.
3738
*/
38-
NODISCARD bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet);
39+
NODISCARD bool DecodeBase58(const char* psz, std::vector<unsigned char>& vchRet, int max_ret_len = std::numeric_limits<int>::max());
3940

4041
/**
4142
* Decode a base58-encoded string (str) into a byte vector (vchRet).
4243
* return true if decoding is successful.
4344
*/
44-
NODISCARD bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet);
45+
NODISCARD bool DecodeBase58(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len = std::numeric_limits<int>::max());
4546

4647
/**
4748
* Encode a byte vector into a base58-encoded string, including checksum
@@ -52,12 +53,12 @@ std::string EncodeBase58Check(const std::vector<unsigned char>& vchIn);
5253
* Decode a base58-encoded string (psz) that includes a checksum into a byte
5354
* vector (vchRet), return true if decoding is successful
5455
*/
55-
NODISCARD bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet);
56+
NODISCARD bool DecodeBase58Check(const char* psz, std::vector<unsigned char>& vchRet, int max_ret_len = std::numeric_limits<int>::max());
5657

5758
/**
5859
* Decode a base58-encoded string (str) that includes a checksum into a byte
5960
* vector (vchRet), return true if decoding is successful
6061
*/
61-
NODISCARD bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet);
62+
NODISCARD bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>& vchRet, int max_ret_len = std::numeric_limits<int>::max());
6263

6364
#endif // BITCOIN_BASE58_H

src/test/base58_tests.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <base58.h>
88
#include <test/util/setup_common.h>
99
#include <util/strencodings.h>
10+
#include <util/vector.h>
1011

1112
#include <univalue.h>
1213

@@ -66,4 +67,20 @@ BOOST_AUTO_TEST_CASE(base58_DecodeBase58)
6667
BOOST_CHECK_EQUAL_COLLECTIONS(result.begin(), result.end(), expected.begin(), expected.end());
6768
}
6869

70+
BOOST_AUTO_TEST_CASE(base58_random_encode_decode)
71+
{
72+
for (int n = 0; n < 1000; ++n) {
73+
unsigned int len = 1 + InsecureRandBits(8);
74+
unsigned int zeroes = InsecureRandBool() ? InsecureRandRange(len + 1) : 0;
75+
auto data = Cat(std::vector<unsigned char>(zeroes, '\000'), g_insecure_rand_ctx.randbytes(len - zeroes));
76+
auto encoded = EncodeBase58Check(data);
77+
std::vector<unsigned char> decoded;
78+
auto ok_too_small = DecodeBase58Check(encoded, decoded, InsecureRandRange(len));
79+
BOOST_CHECK(!ok_too_small);
80+
auto ok = DecodeBase58Check(encoded, decoded, len + InsecureRandRange(257 - len));
81+
BOOST_CHECK(ok);
82+
BOOST_CHECK(data == decoded);
83+
}
84+
}
85+
6986
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)