Skip to content

Commit 552b51e

Browse files
committed
refactor: Add sanity checks in LabelFromValue
1 parent 67e7ba8 commit 552b51e

File tree

4 files changed

+15
-23
lines changed

4 files changed

+15
-23
lines changed

src/wallet/rpc/addresses.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ RPCHelpMan getnewaddress()
4343
}
4444

4545
// Parse the label first so we don't generate a key if there's an error
46-
std::string label;
47-
if (!request.params[0].isNull())
48-
label = LabelFromValue(request.params[0]);
46+
const std::string label{LabelFromValue(request.params[0])};
4947

5048
OutputType output_type = pwallet->m_default_address_type;
5149
if (!request.params[1].isNull()) {
@@ -140,7 +138,7 @@ RPCHelpMan setlabel()
140138
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
141139
}
142140

143-
std::string label = LabelFromValue(request.params[1]);
141+
const std::string label{LabelFromValue(request.params[1])};
144142

145143
if (pwallet->IsMine(dest)) {
146144
pwallet->SetAddressBook(dest, label, "receive");
@@ -258,9 +256,7 @@ RPCHelpMan addmultisigaddress()
258256

259257
LOCK2(pwallet->cs_wallet, spk_man.cs_KeyStore);
260258

261-
std::string label;
262-
if (!request.params[2].isNull())
263-
label = LabelFromValue(request.params[2]);
259+
const std::string label{LabelFromValue(request.params[2])};
264260

265261
int required = request.params[0].getInt<int>();
266262

@@ -662,7 +658,7 @@ RPCHelpMan getaddressesbylabel()
662658

663659
LOCK(pwallet->cs_wallet);
664660

665-
std::string label = LabelFromValue(request.params[0]);
661+
const std::string label{LabelFromValue(request.params[0])};
666662

667663
// Find all addresses that have the given label
668664
UniValue ret(UniValue::VOBJ);

src/wallet/rpc/backup.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,7 @@ RPCHelpMan importprivkey()
140140
EnsureWalletIsUnlocked(*pwallet);
141141

142142
std::string strSecret = request.params[0].get_str();
143-
std::string strLabel;
144-
if (!request.params[1].isNull())
145-
strLabel = LabelFromValue(request.params[1]);
143+
const std::string strLabel{LabelFromValue(request.params[1])};
146144

147145
// Whether to perform rescan after import
148146
if (!request.params[2].isNull())
@@ -233,9 +231,7 @@ RPCHelpMan importaddress()
233231

234232
EnsureLegacyScriptPubKeyMan(*pwallet, true);
235233

236-
std::string strLabel;
237-
if (!request.params[1].isNull())
238-
strLabel = LabelFromValue(request.params[1]);
234+
const std::string strLabel{LabelFromValue(request.params[1])};
239235

240236
// Whether to perform rescan after import
241237
bool fRescan = true;
@@ -426,9 +422,7 @@ RPCHelpMan importpubkey()
426422

427423
EnsureLegacyScriptPubKeyMan(*pwallet, true);
428424

429-
std::string strLabel;
430-
if (!request.params[1].isNull())
431-
strLabel = LabelFromValue(request.params[1]);
425+
const std::string strLabel{LabelFromValue(request.params[1])};
432426

433427
// Whether to perform rescan after import
434428
bool fRescan = true;
@@ -1163,7 +1157,7 @@ static UniValue ProcessImport(CWallet& wallet, const UniValue& data, const int64
11631157
if (internal && data.exists("label")) {
11641158
throw JSONRPCError(RPC_INVALID_PARAMETER, "Internal addresses should not have a label");
11651159
}
1166-
const std::string& label = data.exists("label") ? LabelFromValue(data["label"]) : "";
1160+
const std::string label{LabelFromValue(data["label"])};
11671161
const bool add_keypool = data.exists("keypool") ? data["keypool"].get_bool() : false;
11681162

11691163
// Add to keypool only works with privkeys disabled
@@ -1457,7 +1451,7 @@ static UniValue ProcessDescriptorImport(CWallet& wallet, const UniValue& data, c
14571451
const std::string& descriptor = data["desc"].get_str();
14581452
const bool active = data.exists("active") ? data["active"].get_bool() : false;
14591453
const bool internal = data.exists("internal") ? data["internal"].get_bool() : false;
1460-
const std::string& label = data.exists("label") ? LabelFromValue(data["label"]) : "";
1454+
const std::string label{LabelFromValue(data["label"])};
14611455

14621456
// Parse descriptor string
14631457
FlatSigningProvider keys;

src/wallet/rpc/transactions.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,10 +635,9 @@ RPCHelpMan listsinceblock()
635635
bool include_removed = (request.params[3].isNull() || request.params[3].get_bool());
636636
bool include_change = (!request.params[4].isNull() && request.params[4].get_bool());
637637

638+
// Only set it if 'label' was provided.
638639
std::optional<std::string> filter_label;
639-
if (!request.params[5].isNull()) {
640-
filter_label = LabelFromValue(request.params[5]);
641-
}
640+
if (!request.params[5].isNull()) filter_label.emplace(LabelFromValue(request.params[5]));
642641

643642
int depth = height ? wallet.GetLastBlockHeight() + 1 - *height : -1;
644643

src/wallet/rpc/util.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ const LegacyScriptPubKeyMan& EnsureConstLegacyScriptPubKeyMan(const CWallet& wal
132132

133133
std::string LabelFromValue(const UniValue& value)
134134
{
135-
std::string label = value.get_str();
135+
static const std::string empty_string;
136+
if (value.isNull()) return empty_string;
137+
138+
const std::string& label{value.get_str()};
136139
if (label == "*")
137140
throw JSONRPCError(RPC_WALLET_INVALID_LABEL_NAME, "Invalid label name");
138141
return label;

0 commit comments

Comments
 (0)