aboutsummaryrefslogtreecommitdiff
path: root/src/rpc/util.cpp
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2019-02-25 09:29:50 +0100
committerWladimir J. van der Laan <laanwj@gmail.com>2019-02-25 09:31:08 +0100
commit1a8a5ede9fc334bd97b774670eb340b8665a0aa4 (patch)
treec3449c48d35f665b8de6da0888e87e2b8a6e62b7 /src/rpc/util.cpp
parentf3f9c1de19e6d254e0c3a26ce7a3d8cd57fb7641 (diff)
parentfa4ce7038d444defe0b98a30097174c278054a33 (diff)
downloadbitcoin-1a8a5ede9fc334bd97b774670eb340b8665a0aa4.tar.xz
Merge #15401: rpc: Actually throw help when passed invalid number of params
fa4ce7038d444defe0b98a30097174c278054a33 rpc: Actually throw help when passed invalid number of params (MarcoFalke) fa05626ca7a0fe896ac554c79eaea4c36acdf861 rpc: Add RPCHelpMan::IsValidNumArgs() (MarcoFalke) Pull request description: Can be tested by * running the included test against an old binary (compiled without this patch) * calling `setban 1 "add" 3 4 5 6 7 8 9 0` in the gui Tree-SHA512: aa6a25bbe6f40722913ea292252a62a4012c964eed9f4035335a2e2d13be98eb60f368e8a3251a104a26a62c08b2cb926b06e5ab1418ef1cf4abdd71d87c2919
Diffstat (limited to 'src/rpc/util.cpp')
-rw-r--r--src/rpc/util.cpp29
1 files changed, 22 insertions, 7 deletions
diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp
index 1eec916abf..86695bc1a5 100644
--- a/src/rpc/util.cpp
+++ b/src/rpc/util.cpp
@@ -1,4 +1,4 @@
-// Copyright (c) 2017-2018 The Bitcoin Core developers
+// Copyright (c) 2017-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
@@ -314,6 +314,17 @@ std::string RPCExamples::ToDescriptionString() const
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 ret;
@@ -322,12 +333,7 @@ std::string RPCHelpMan::ToString() const
ret += m_name;
bool was_optional{false};
for (const auto& arg : m_args) {
- bool optional;
- if (arg.m_fallback.which() == 1) {
- optional = true;
- } else {
- optional = RPCArg::Optional::NO != boost::get<RPCArg::Optional>(arg.m_fallback);
- }
+ const bool optional = arg.IsOptional();
ret += " ";
if (optional) {
if (!was_optional) ret += "( ";
@@ -369,6 +375,15 @@ std::string RPCHelpMan::ToString() const
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 ret;