Skip to content

Commit fa4ce70

Browse files
author
MarcoFalke
committed
rpc: Actually throw help when passed invalid number of params
1 parent fa05626 commit fa4ce70

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

src/rpc/blockchain.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2018 The Bitcoin Core developers
2+
// Copyright (c) 2009-2019 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -1778,9 +1778,7 @@ static constexpr size_t PER_UTXO_OVERHEAD = sizeof(COutPoint) + sizeof(uint32_t)
17781778

17791779
static UniValue getblockstats(const JSONRPCRequest& request)
17801780
{
1781-
if (request.fHelp || request.params.size() < 1 || request.params.size() > 4) {
1782-
throw std::runtime_error(
1783-
RPCHelpMan{"getblockstats",
1781+
const RPCHelpMan help{"getblockstats",
17841782
"\nCompute per block statistics for a given window. All amounts are in satoshis.\n"
17851783
"It won't work for some heights with pruning.\n"
17861784
"It won't work without -txindex for utxo_size_inc, *fee or *feerate stats.\n",
@@ -1836,7 +1834,9 @@ static UniValue getblockstats(const JSONRPCRequest& request)
18361834
HelpExampleCli("getblockstats", "1000 '[\"minfeerate\",\"avgfeerate\"]'")
18371835
+ HelpExampleRpc("getblockstats", "1000 '[\"minfeerate\",\"avgfeerate\"]'")
18381836
},
1839-
}.ToString());
1837+
};
1838+
if (request.fHelp || !help.IsValidNumArgs(request.params.size())) {
1839+
throw std::runtime_error(help.ToString());
18401840
}
18411841

18421842
LOCK(cs_main);

src/rpc/net.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -523,13 +523,7 @@ static UniValue getnetworkinfo(const JSONRPCRequest& request)
523523

524524
static UniValue setban(const JSONRPCRequest& request)
525525
{
526-
std::string strCommand;
527-
if (!request.params[1].isNull())
528-
strCommand = request.params[1].get_str();
529-
if (request.fHelp || request.params.size() < 2 ||
530-
(strCommand != "add" && strCommand != "remove"))
531-
throw std::runtime_error(
532-
RPCHelpMan{"setban",
526+
const RPCHelpMan help{"setban",
533527
"\nAttempts to add or remove an IP/Subnet from the banned list.\n",
534528
{
535529
{"subnet", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP/Subnet (see getpeerinfo for nodes IP) with an optional netmask (default is /32 = single IP)"},
@@ -543,7 +537,13 @@ static UniValue setban(const JSONRPCRequest& request)
543537
+ HelpExampleCli("setban", "\"192.168.0.0/24\" \"add\"")
544538
+ HelpExampleRpc("setban", "\"192.168.0.6\", \"add\", 86400")
545539
},
546-
}.ToString());
540+
};
541+
std::string strCommand;
542+
if (!request.params[1].isNull())
543+
strCommand = request.params[1].get_str();
544+
if (request.fHelp || !help.IsValidNumArgs(request.params.size()) || (strCommand != "add" && strCommand != "remove")) {
545+
throw std::runtime_error(help.ToString());
546+
}
547547
if (!g_banman) {
548548
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
549549
}

test/functional/rpc_getblockstats.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,5 +178,10 @@ def run_test(self):
178178
assert_raises_rpc_error(-5, 'Block not found', self.nodes[0].getblockstats,
179179
hash_or_height='000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f')
180180

181+
# Invalid number of args
182+
assert_raises_rpc_error(-1, 'getblockstats hash_or_height ( stats )', self.nodes[0].getblockstats, '00', 1, 2)
183+
assert_raises_rpc_error(-1, 'getblockstats hash_or_height ( stats )', self.nodes[0].getblockstats)
184+
185+
181186
if __name__ == '__main__':
182187
GetblockstatsTest().main()

0 commit comments

Comments
 (0)