Skip to content

Commit 21b42f3

Browse files
committed
Merge bitcoin/bitcoin#32660: rpc: Use type-safe exception to pass RPC help
fa94652 refactor: Use structured binding for-loop (MarcoFalke) eeeec15 rpc: Use type-safe exception to pass RPC help (MarcoFalke) Pull request description: The current "catch-all" `catch (const std::exception& e)` in `CRPCTable::help` is problematic, because it could catch exceptions unrelated to passing the help string up. Fix this by using a dedicated exception type. ACKs for top commit: l0rinc: tested ACK fa94652 (edited) achow101: ACK fa94652 rkrux: re-ACK fa94652 Tree-SHA512: 23dac6e0fe925561bfbf421e6a7441d546eed8c1492ac41ca4ed7dfcd12f4d2ef39c35f105a0291aac511365d98f08fbdc9a4f0bf627172873b8f23c2be45e76
2 parents 528f79f + fa94652 commit 21b42f3

File tree

3 files changed

+11
-11
lines changed

3 files changed

+11
-11
lines changed

src/rpc/server.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2022 The Bitcoin Core developers
2+
// Copyright (c) 2009-present 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

@@ -21,6 +21,7 @@
2121
#include <util/time.h>
2222
#include <validation.h>
2323

24+
#include <algorithm>
2425
#include <cassert>
2526
#include <chrono>
2627
#include <memory>
@@ -79,15 +80,13 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
7980

8081
for (const auto& entry : mapCommands)
8182
vCommands.emplace_back(entry.second.front()->category + entry.first, entry.second.front());
82-
sort(vCommands.begin(), vCommands.end());
83+
std::ranges::sort(vCommands);
8384

8485
JSONRPCRequest jreq = helpreq;
8586
jreq.mode = JSONRPCRequest::GET_HELP;
8687
jreq.params = UniValue();
8788

88-
for (const std::pair<std::string, const CRPCCommand*>& command : vCommands)
89-
{
90-
const CRPCCommand *pcmd = command.second;
89+
for (const auto& [_, pcmd] : vCommands) {
9190
std::string strMethod = pcmd->name;
9291
if ((strCommand != "" || pcmd->category == "hidden") && strMethod != strCommand)
9392
continue;
@@ -97,11 +96,8 @@ std::string CRPCTable::help(const std::string& strCommand, const JSONRPCRequest&
9796
UniValue unused_result;
9897
if (setDone.insert(pcmd->unique_id).second)
9998
pcmd->actor(jreq, unused_result, /*last_handler=*/true);
100-
}
101-
catch (const std::exception& e)
102-
{
103-
// Help text is returned in an exception
104-
std::string strHelp = std::string(e.what());
99+
} catch (const HelpResult& e) {
100+
std::string strHelp{e.what()};
105101
if (strCommand == "")
106102
{
107103
if (strHelp.find('\n') != std::string::npos)

src/rpc/util.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ UniValue RPCHelpMan::HandleRequest(const JSONRPCRequest& request) const
645645
* the user is asking for help information, and throw help when appropriate.
646646
*/
647647
if (request.mode == JSONRPCRequest::GET_HELP || !IsValidNumArgs(request.params.size())) {
648-
throw std::runtime_error(ToString());
648+
throw HelpResult{ToString()};
649649
}
650650
UniValue arg_mismatch{UniValue::VOBJ};
651651
for (size_t i{0}; i < m_args.size(); ++i) {

src/rpc/util.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ class FillableSigningProvider;
6767
class CScript;
6868
struct Sections;
6969

70+
struct HelpResult : std::runtime_error {
71+
explicit HelpResult(const std::string& msg) : std::runtime_error{msg} {}
72+
};
73+
7074
/**
7175
* Gets all existing output types formatted for RPC help sections.
7276
*

0 commit comments

Comments
 (0)