From eeeec1579ec5a3aa7b10ff62f87d197ae311a666 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Mon, 2 Jun 2025 13:46:42 +0200 Subject: [PATCH 1/2] rpc: Use type-safe exception to pass RPC help --- src/rpc/server.cpp | 7 ++----- src/rpc/util.cpp | 2 +- src/rpc/util.h | 4 ++++ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 8631ae0adfe..8d120f41ef4 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -97,11 +97,8 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest& UniValue unused_result; if (setDone.insert(pcmd->unique_id).second) pcmd->actor(jreq, unused_result, /*last_handler=*/true); - } - catch (const std::exception& e) - { - // Help text is returned in an exception - std::string strHelp = std::string(e.what()); + } catch (const HelpResult& e) { + std::string strHelp{e.what()}; if (strCommand == "") { if (strHelp.find('\n') != std::string::npos) diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index 5ddc02c94ee..5da02b4df4e 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -645,7 +645,7 @@ UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request) const * the user is asking for help information, and throw help when appropriate. */ if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) { - throw std::runtime_error(ToString()); + throw HelpResult{ToString()}; } UniValue arg_mismatch{UniValue::VOBJ}; for (size_t i{0}; i < m_args.size(); ++i) { diff --git a/src/rpc/util.h b/src/rpc/util.h index b2387a4c8ef..1650d2da1f6 100644 --- a/src/rpc/util.h +++ b/src/rpc/util.h @@ -67,6 +67,10 @@ class FillableSigningProvider; class CScript; struct Sections; +struct HelpResult : std::runtime_error { + explicit HelpResult(const std::string& msg) : std::runtime_error{msg} {} +}; + /** * Gets all existing output types formatted for RPC help sections. * From fa946520d229ae45b30519bccc9eaa2c47b4a093 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Mon, 2 Jun 2025 14:16:06 +0200 Subject: [PATCH 2/2] refactor: Use structured binding for-loop --- src/rpc/server.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index 8d120f41ef4..bd99615bbf0 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -79,15 +80,13 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest& for (const auto& entry : mapCommands) vCommands.emplace_back(entry.second.front()->category + entry.first, entry.second.front()); - sort(vCommands.begin(), vCommands.end()); + std::ranges::sort(vCommands); JSONRPCRequest jreq = helpreq; jreq.mode = JSONRPCRequest::GET_HELP; jreq.params = UniValue(); - for (const std::pair& command : vCommands) - { - const CRPCCommand *pcmd = command.second; + for (const auto& [_, pcmd] : vCommands) { std::string strMethod = pcmd->name; if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand) continue;