Skip to content

Commit fabef12

Browse files
author
MarcoFalke
committed
refactor: Use EnsureAnyNodeContext
node_context is never null, but if it was, it would lead to a nullptr dereference in node_context->scheduler. Just use EnsureAnyNodeContext everywhere for more robust, consistent, and correct code.
1 parent fa16406 commit fabef12

File tree

1 file changed

+5
-9
lines changed

1 file changed

+5
-9
lines changed

src/rpc/node.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,9 @@ static RPCHelpMan setmocktime()
5757
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Mocktime cannot be negative: %s.", time));
5858
}
5959
SetMockTime(time);
60-
auto node_context = util::AnyPtr<NodeContext>(request.context);
61-
if (node_context) {
62-
for (const auto& chain_client : node_context->chain_clients) {
63-
chain_client->setMockTime(time);
64-
}
60+
const NodeContext& node_context{EnsureAnyNodeContext(request.context)};
61+
for (const auto& chain_client : node_context.chain_clients) {
62+
chain_client->setMockTime(time);
6563
}
6664

6765
return UniValue::VNULL;
@@ -89,10 +87,8 @@ static RPCHelpMan mockscheduler()
8987
throw std::runtime_error("delta_time must be between 1 and 3600 seconds (1 hr)");
9088
}
9189

92-
auto node_context = CHECK_NONFATAL(util::AnyPtr<NodeContext>(request.context));
93-
// protect against null pointer dereference
94-
CHECK_NONFATAL(node_context->scheduler);
95-
node_context->scheduler->MockForward(std::chrono::seconds(delta_seconds));
90+
const NodeContext& node_context{EnsureAnyNodeContext(request.context)};
91+
CHECK_NONFATAL(node_context.scheduler)->MockForward(std::chrono::seconds{delta_seconds});
9692
SyncWithValidationInterfaceQueue();
9793

9894
return UniValue::VNULL;

0 commit comments

Comments
 (0)