merge bitcoin#15401: Actually throw help when passed invalid number of params

This commit is contained in:
Kittywhiskers Van Gogh 2021-12-12 19:08:12 +05:30
parent 269501259c
commit 2fecd1495f
5 changed files with 46 additions and 22 deletions

View File

@ -1,5 +1,5 @@
// Copyright (c) 2010 Satoshi Nakamoto // Copyright (c) 2010 Satoshi Nakamoto
// Copyright (c) 2009-2015 The Bitcoin Core developers // Copyright (c) 2009-2019 The Bitcoin Core developers
// Copyright (c) 2014-2021 The Dash Core developers // Copyright (c) 2014-2021 The Dash Core developers
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -2026,9 +2026,7 @@ static constexpr size_t PER_UTXO_OVERHEAD = sizeof(COutPoint) + sizeof(uint32_t)
static UniValue getblockstats(const JSONRPCRequest& request) static UniValue getblockstats(const JSONRPCRequest& request)
{ {
if (request.fHelp || request.params.size() < 1 || request.params.size() > 4) { const RPCHelpMan help{"getblockstats",
throw std::runtime_error(
RPCHelpMan{"getblockstats",
"\nCompute per block statistics for a given window. All amounts are in duffs.\n" "\nCompute per block statistics for a given window. All amounts are in duffs.\n"
"It won't work for some heights with pruning.\n" "It won't work for some heights with pruning.\n"
"It won't work without -txindex for utxo_size_inc, *fee or *feerate stats.\n", "It won't work without -txindex for utxo_size_inc, *fee or *feerate stats.\n",
@ -2080,7 +2078,9 @@ static UniValue getblockstats(const JSONRPCRequest& request)
HelpExampleCli("getblockstats", "1000 '[\"minfeerate\",\"avgfeerate\"]'") HelpExampleCli("getblockstats", "1000 '[\"minfeerate\",\"avgfeerate\"]'")
+ HelpExampleRpc("getblockstats", "1000 '[\"minfeerate\",\"avgfeerate\"]'") + HelpExampleRpc("getblockstats", "1000 '[\"minfeerate\",\"avgfeerate\"]'")
}, },
}.ToString()); };
if (request.fHelp || !help.IsValidNumArgs(request.params.size())) {
throw std::runtime_error(help.ToString());
} }
if (g_txindex) { if (g_txindex) {

View File

@ -584,13 +584,7 @@ static UniValue getnetworkinfo(const JSONRPCRequest& request)
static UniValue setban(const JSONRPCRequest& request) static UniValue setban(const JSONRPCRequest& request)
{ {
std::string strCommand; const RPCHelpMan help{"setban",
if (!request.params[1].isNull())
strCommand = request.params[1].get_str();
if (request.fHelp || request.params.size() < 2 ||
(strCommand != "add" && strCommand != "remove"))
throw std::runtime_error(
RPCHelpMan{"setban",
"\nAttempts to add or remove an IP/Subnet from the banned list.\n", "\nAttempts to add or remove an IP/Subnet from the banned list.\n",
{ {
{"subnet", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP/Subnet (see getpeerinfo for nodes IP) with an optional netmask (default is /32 = single IP)"}, {"subnet", RPCArg::Type::STR, RPCArg::Optional::NO, "The IP/Subnet (see getpeerinfo for nodes IP) with an optional netmask (default is /32 = single IP)"},
@ -604,7 +598,13 @@ static UniValue setban(const JSONRPCRequest& request)
+ HelpExampleCli("setban", "\"192.168.0.0/24\" \"add\"") + HelpExampleCli("setban", "\"192.168.0.0/24\" \"add\"")
+ HelpExampleRpc("setban", "\"192.168.0.6\", \"add\", 86400") + HelpExampleRpc("setban", "\"192.168.0.6\", \"add\", 86400")
}, },
}.ToString()); };
std::string strCommand;
if (!request.params[1].isNull())
strCommand = request.params[1].get_str();
if (request.fHelp || !help.IsValidNumArgs(request.params.size()) || (strCommand != "add" && strCommand != "remove")) {
throw std::runtime_error(help.ToString());
}
if (!g_banman) { if (!g_banman) {
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded"); throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
} }

View File

@ -1,4 +1,4 @@
// Copyright (c) 2017 The Bitcoin Core developers // Copyright (c) 2017-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -407,6 +407,17 @@ std::string RPCExamples::ToDescriptionString() const
return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples; return m_examples.empty() ? m_examples : "\nExamples:\n" + m_examples;
} }
bool RPCHelpMan::IsValidNumArgs(size_t num_args) const
{
size_t num_required_args = 0;
for (size_t n = m_args.size(); n > 0; --n) {
if (!m_args.at(n - 1).IsOptional()) {
num_required_args = n;
break;
}
}
return num_required_args <= num_args && num_args <= m_args.size();
}
std::string RPCHelpMan::ToString() const std::string RPCHelpMan::ToString() const
{ {
std::string ret; std::string ret;
@ -415,12 +426,7 @@ std::string RPCHelpMan::ToString() const
ret += m_name; ret += m_name;
bool was_optional{false}; bool was_optional{false};
for (const auto& arg : m_args) { for (const auto& arg : m_args) {
bool optional; const bool optional = arg.IsOptional();
if (arg.m_fallback.which() == 1) {
optional = true;
} else {
optional = RPCArg::Optional::NO != boost::get<RPCArg::Optional>(arg.m_fallback);
}
ret += " "; ret += " ";
if (optional) { if (optional) {
if (!was_optional) ret += "( "; if (!was_optional) ret += "( ";
@ -462,6 +468,15 @@ std::string RPCHelpMan::ToString() const
return ret; return ret;
} }
bool RPCArg::IsOptional() const
{
if (m_fallback.which() == 1) {
return true;
} else {
return RPCArg::Optional::NO != boost::get<RPCArg::Optional>(m_fallback);
}
}
std::string RPCArg::ToDescriptionString() const std::string RPCArg::ToDescriptionString() const
{ {
std::string ret; std::string ret;

View File

@ -1,4 +1,4 @@
// Copyright (c) 2017 The Bitcoin Core developers // Copyright (c) 2017-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -103,7 +103,7 @@ struct RPCArg {
/** Required arg */ /** Required arg */
NO, NO,
/** /**
* Optinal arg that is a named argument and has a default value of * Optional arg that is a named argument and has a default value of
* `null`. When possible, the default value should be specified. * `null`. When possible, the default value should be specified.
*/ */
OMITTED_NAMED_ARG, OMITTED_NAMED_ARG,
@ -160,6 +160,8 @@ struct RPCArg {
assert(type == Type::ARR || type == Type::OBJ); assert(type == Type::ARR || type == Type::OBJ);
} }
bool IsOptional() const;
/** /**
* Return the type string of the argument. * Return the type string of the argument.
* Set oneline to allow it to be overridden by a custom oneline type string (m_oneline_description). * Set oneline to allow it to be overridden by a custom oneline type string (m_oneline_description).
@ -235,6 +237,8 @@ public:
RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples); RPCHelpMan(std::string name, std::string description, std::vector<RPCArg> args, RPCResults results, RPCExamples examples);
std::string ToString() const; std::string ToString() const;
/** If the supplied number of args is neither too small nor too high */
bool IsValidNumArgs(size_t num_args) const;
private: private:
const std::string m_name; const std::string m_name;

View File

@ -177,5 +177,10 @@ class GetblockstatsTest(BitcoinTestFramework):
assert_raises_rpc_error(-5, 'Block not found', self.nodes[0].getblockstats, assert_raises_rpc_error(-5, 'Block not found', self.nodes[0].getblockstats,
hash_or_height='000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f') hash_or_height='000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f')
# Invalid number of args
assert_raises_rpc_error(-1, 'getblockstats hash_or_height ( stats )', self.nodes[0].getblockstats, '00', 1, 2)
assert_raises_rpc_error(-1, 'getblockstats hash_or_height ( stats )', self.nodes[0].getblockstats)
if __name__ == '__main__': if __name__ == '__main__':
GetblockstatsTest().main() GetblockstatsTest().main()