Skip to content

Commit 78f3ac5

Browse files
sipaMacroFake
authored andcommitted
Make DecodeBase{32,64} return optional instead of taking bool*
1 parent a65931e commit 78f3ac5

File tree

11 files changed

+57
-77
lines changed

11 files changed

+57
-77
lines changed

src/httprpc.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ static bool RPCAuthorized(const std::string& strAuth, std::string& strAuthUserna
132132
if (strAuth.substr(0, 6) != "Basic ")
133133
return false;
134134
std::string strUserPass64 = TrimString(strAuth.substr(6));
135-
bool invalid;
136-
std::vector<unsigned char> userpass_data = DecodeBase64(strUserPass64, &invalid);
137-
if (invalid) return false;
138-
std::string strUserPass(userpass_data.begin(), userpass_data.end());
135+
auto userpass_data = DecodeBase64(strUserPass64);
136+
std::string strUserPass;
137+
if (!userpass_data) return false;
138+
strUserPass.assign(userpass_data->begin(), userpass_data->end());
139139

140140
if (strUserPass.find(':') != std::string::npos)
141141
strAuthUsernameOut = strUserPass.substr(0, strUserPass.find(':'));

src/i2p.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,11 @@ static std::string SwapBase64(const std::string& from)
6969
static Binary DecodeI2PBase64(const std::string& i2p_b64)
7070
{
7171
const std::string& std_b64 = SwapBase64(i2p_b64);
72-
bool invalid;
73-
Binary decoded = DecodeBase64(std_b64, &invalid);
74-
if (invalid) {
72+
auto decoded = DecodeBase64(std_b64);
73+
if (!decoded) {
7574
throw std::runtime_error(strprintf("Cannot decode Base64: \"%s\"", i2p_b64));
7675
}
77-
return decoded;
76+
return std::move(*decoded);
7877
}
7978

8079
/**

src/netaddress.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,16 @@ bool CNetAddr::SetTor(const std::string& addr)
234234
return false;
235235
}
236236

237-
bool invalid;
238-
const auto& input = DecodeBase32(addr.substr(0, addr.size() - suffix_len), &invalid);
237+
auto input = DecodeBase32(addr.substr(0, addr.size() - suffix_len));
239238

240-
if (invalid) {
239+
if (!input) {
241240
return false;
242241
}
243242

244-
if (input.size() == torv3::TOTAL_LEN) {
245-
Span<const uint8_t> input_pubkey{input.data(), ADDR_TORV3_SIZE};
246-
Span<const uint8_t> input_checksum{input.data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
247-
Span<const uint8_t> input_version{input.data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
243+
if (input->size() == torv3::TOTAL_LEN) {
244+
Span<const uint8_t> input_pubkey{input->data(), ADDR_TORV3_SIZE};
245+
Span<const uint8_t> input_checksum{input->data() + ADDR_TORV3_SIZE, torv3::CHECKSUM_LEN};
246+
Span<const uint8_t> input_version{input->data() + ADDR_TORV3_SIZE + torv3::CHECKSUM_LEN, sizeof(torv3::VERSION)};
248247

249248
if (input_version != torv3::VERSION) {
250249
return false;
@@ -280,15 +279,14 @@ bool CNetAddr::SetI2P(const std::string& addr)
280279
// can decode it.
281280
const std::string b32_padded = addr.substr(0, b32_len) + "====";
282281

283-
bool invalid;
284-
const auto& address_bytes = DecodeBase32(b32_padded.c_str(), &invalid);
282+
auto address_bytes = DecodeBase32(b32_padded);
285283

286-
if (invalid || address_bytes.size() != ADDR_I2P_SIZE) {
284+
if (!address_bytes || address_bytes->size() != ADDR_I2P_SIZE) {
287285
return false;
288286
}
289287

290288
m_net = NET_I2P;
291-
m_addr.assign(address_bytes.begin(), address_bytes.end());
289+
m_addr.assign(address_bytes->begin(), address_bytes->end());
292290

293291
return true;
294292
}

src/psbt.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,12 @@ std::string PSBTRoleName(PSBTRole role) {
388388

389389
bool DecodeBase64PSBT(PartiallySignedTransaction& psbt, const std::string& base64_tx, std::string& error)
390390
{
391-
bool invalid;
392-
auto tx_data = DecodeBase64(base64_tx, &invalid);
393-
if (invalid) {
391+
auto tx_data = DecodeBase64(base64_tx);
392+
if (!tx_data) {
394393
error = "invalid base64";
395394
return false;
396395
}
397-
return DecodeRawPSBT(psbt, MakeByteSpan(tx_data), error);
396+
return DecodeRawPSBT(psbt, MakeByteSpan(*tx_data), error);
398397
}
399398

400399
bool DecodeRawPSBT(PartiallySignedTransaction& psbt, Span<const std::byte> tx_data, std::string& error)

src/qt/walletframe.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,12 @@ void WalletFrame::gotoLoadPSBT(bool from_clipboard)
198198

199199
if (from_clipboard) {
200200
std::string raw = QApplication::clipboard()->text().toStdString();
201-
bool invalid;
202-
data = DecodeBase64(raw, &invalid);
203-
if (invalid) {
201+
auto result = DecodeBase64(raw);
202+
if (!result) {
204203
Q_EMIT message(tr("Error"), tr("Unable to decode PSBT from clipboard (invalid base64)"), CClientUIInterface::MSG_ERROR);
205204
return;
206205
}
206+
data = std::move(*result);
207207
} else {
208208
QString filename = GUIUtil::getOpenFileName(this,
209209
tr("Load Transaction Data"), QString(),

src/test/base32_tests.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,16 @@ BOOST_AUTO_TEST_CASE(base32_testvectors)
2222
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
2323
strEnc = EncodeBase32(vstrIn[i], false);
2424
BOOST_CHECK_EQUAL(strEnc, vstrOutNoPadding[i]);
25-
bool invalid;
26-
auto dec = DecodeBase32(vstrOut[i], &invalid);
27-
BOOST_CHECK(!invalid);
28-
BOOST_CHECK_MESSAGE(MakeByteSpan(dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
25+
auto dec = DecodeBase32(vstrOut[i]);
26+
BOOST_REQUIRE(dec);
27+
BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
2928
}
3029

3130
// Decoding strings with embedded NUL characters should fail
32-
bool failure;
33-
(void)DecodeBase32("invalid\0"s, &failure); // correct size, invalid due to \0
34-
BOOST_CHECK(failure);
35-
(void)DecodeBase32("AWSX3VPP"s, &failure); // valid
36-
BOOST_CHECK(!failure);
37-
(void)DecodeBase32("AWSX3VPP\0invalid"s, &failure); // correct size, invalid due to \0
38-
BOOST_CHECK(failure);
39-
(void)DecodeBase32("AWSX3VPPinvalid"s, &failure); // invalid size
40-
BOOST_CHECK(failure);
31+
BOOST_CHECK(!DecodeBase32("invalid\0"s)); // correct size, invalid due to \0
32+
BOOST_CHECK(DecodeBase32("AWSX3VPP"s)); // valid
33+
BOOST_CHECK(!DecodeBase32("AWSX3VPP\0invalid"s)); // correct size, invalid due to \0
34+
BOOST_CHECK(!DecodeBase32("AWSX3VPPinvalid"s)); // invalid size
4135
}
4236

4337
BOOST_AUTO_TEST_SUITE_END()

src/test/base64_tests.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
1919
{
2020
std::string strEnc = EncodeBase64(vstrIn[i]);
2121
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);
22-
bool invalid;
23-
auto dec = DecodeBase64(strEnc, &invalid);
24-
BOOST_CHECK(!invalid);
25-
BOOST_CHECK_MESSAGE(MakeByteSpan(dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
22+
auto dec = DecodeBase64(strEnc);
23+
BOOST_REQUIRE(dec);
24+
BOOST_CHECK_MESSAGE(MakeByteSpan(*dec) == MakeByteSpan(vstrIn[i]), vstrOut[i]);
2625
}
2726

2827
{
@@ -36,15 +35,10 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
3635
}
3736

3837
// Decoding strings with embedded NUL characters should fail
39-
bool failure;
40-
(void)DecodeBase64("invalid\0"s, &failure);
41-
BOOST_CHECK(failure);
42-
(void)DecodeBase64("nQB/pZw="s, &failure);
43-
BOOST_CHECK(!failure);
44-
(void)DecodeBase64("nQB/pZw=\0invalid"s, &failure);
45-
BOOST_CHECK(failure);
46-
(void)DecodeBase64("nQB/pZw=invalid\0"s, &failure);
47-
BOOST_CHECK(failure);
38+
BOOST_CHECK(!DecodeBase64("invalid\0"s));
39+
BOOST_CHECK(DecodeBase64("nQB/pZw="s));
40+
BOOST_CHECK(!DecodeBase64("nQB/pZw=\0invalid"s));
41+
BOOST_CHECK(!DecodeBase64("nQB/pZw=invalid\0"s));
4842
}
4943

5044
BOOST_AUTO_TEST_SUITE_END()

src/test/fuzz/base_encode_decode.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,16 @@ FUZZ_TARGET_INIT(base_encode_decode, initialize_base_encode_decode)
3636
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
3737
}
3838

39-
bool pf_invalid;
40-
decoded = DecodeBase32(random_encoded_string, &pf_invalid);
41-
if (!pf_invalid) {
42-
const std::string encoded_string = EncodeBase32(decoded);
39+
auto result = DecodeBase32(random_encoded_string);
40+
if (result) {
41+
const std::string encoded_string = EncodeBase32(*result);
4342
assert(encoded_string == TrimString(encoded_string));
4443
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
4544
}
4645

47-
decoded = DecodeBase64(random_encoded_string, &pf_invalid);
48-
if (!pf_invalid) {
49-
const std::string encoded_string = EncodeBase64(decoded);
46+
result = DecodeBase64(random_encoded_string);
47+
if (result) {
48+
const std::string encoded_string = EncodeBase64(*result);
5049
assert(encoded_string == TrimString(encoded_string));
5150
assert(ToLower(encoded_string) == ToLower(TrimString(random_encoded_string)));
5251
}

src/util/message.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,13 @@ MessageVerificationResult MessageVerify(
3535
return MessageVerificationResult::ERR_ADDRESS_NO_KEY;
3636
}
3737

38-
bool invalid = false;
39-
std::vector<unsigned char> signature_bytes = DecodeBase64(signature, &invalid);
40-
if (invalid) {
38+
auto signature_bytes = DecodeBase64(signature);
39+
if (!signature_bytes) {
4140
return MessageVerificationResult::ERR_MALFORMED_SIGNATURE;
4241
}
4342

4443
CPubKey pubkey;
45-
if (!pubkey.RecoverCompact(MessageHash(message), signature_bytes)) {
44+
if (!pubkey.RecoverCompact(MessageHash(message), *signature_bytes)) {
4645
return MessageVerificationResult::ERR_PUBKEY_NOT_RECOVERED;
4746
}
4847

src/util/strencodings.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ std::string EncodeBase64(Span<const unsigned char> input)
126126
return str;
127127
}
128128

129-
std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
129+
std::optional<std::vector<unsigned char>> DecodeBase64(const char* p)
130130
{
131131
static const int8_t decode64_table[256]{
132132
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -167,18 +167,17 @@ std::vector<unsigned char> DecodeBase64(const char* p, bool* pf_invalid)
167167
++p;
168168
}
169169
valid = valid && (p - e) % 4 == 0 && p - q < 4;
170-
*pf_invalid = !valid;
170+
if (!valid) return {};
171171

172172
return ret;
173173
}
174174

175-
std::vector<unsigned char> DecodeBase64(const std::string& str, bool* pf_invalid)
175+
std::optional<std::vector<unsigned char>> DecodeBase64(const std::string& str)
176176
{
177177
if (!ValidAsCString(str)) {
178-
*pf_invalid = true;
179178
return {};
180179
}
181-
return DecodeBase64(str.c_str(), pf_invalid);
180+
return DecodeBase64(str.c_str());
182181
}
183182

184183
std::string EncodeBase32(Span<const unsigned char> input, bool pad)
@@ -201,7 +200,7 @@ std::string EncodeBase32(const std::string& str, bool pad)
201200
return EncodeBase32(MakeUCharSpan(str), pad);
202201
}
203202

204-
std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)
203+
std::optional<std::vector<unsigned char>> DecodeBase32(const char* p)
205204
{
206205
static const int8_t decode32_table[256]{
207206
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -242,18 +241,17 @@ std::vector<unsigned char> DecodeBase32(const char* p, bool* pf_invalid)
242241
++p;
243242
}
244243
valid = valid && (p - e) % 8 == 0 && p - q < 8;
245-
*pf_invalid = !valid;
244+
if (!valid) return {};
246245

247246
return ret;
248247
}
249248

250-
std::vector<unsigned char> DecodeBase32(const std::string& str, bool* pf_invalid)
249+
std::optional<std::vector<unsigned char>> DecodeBase32(const std::string& str)
251250
{
252251
if (!ValidAsCString(str)) {
253-
*pf_invalid = true;
254252
return {};
255253
}
256-
return DecodeBase32(str.c_str(), pf_invalid);
254+
return DecodeBase32(str.c_str());
257255
}
258256

259257
namespace {

0 commit comments

Comments
 (0)