If you’re taking a pointer, you should check that it’s non-null before dereferencing it. Even better, pass by reference to communicate that this must be called with a valid CBlockIndex
:
0diff --git a/src/net_processing.cpp b/src/net_processing.cpp
1index a822960dc2..f552aa36b5 100644
2--- a/src/net_processing.cpp
3+++ b/src/net_processing.cpp
4@@ -246,7 +246,7 @@ public:
5
6 /** Implement PeerManager */
7 void CheckForStaleTipAndEvictPeers() override;
8- bool FetchBlock(NodeId id, const CBlockIndex* index) override;
9+ bool FetchBlock(NodeId id, const CBlockIndex& index) override;
10 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override;
11 bool IgnoresIncomingTxs() override { return m_ignore_incoming_txs; }
12 void SendPings() override;
13@@ -1253,7 +1253,7 @@ bool PeerManagerImpl::BlockRequestAllowed(const CBlockIndex* pindex)
14 (GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, m_chainparams.GetConsensus()) < STALE_RELAY_AGE_LIMIT);
15 }
16
17-bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex* index)
18+bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex& index)
19 {
20 PeerRef peer = GetPeerRef(id);
21 if (peer == nullptr) return false;
22@@ -1263,12 +1263,12 @@ bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex* index)
23 if (state == nullptr) return false;
24 if (!state->fHaveWitness) return false;
25 const int node_sync_height = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
26- if (index->nHeight > node_sync_height) {
27+ if (index.nHeight > node_sync_height) {
28 return false;
29 }
30- std::vector<CInv> invs{CInv(MSG_BLOCK | MSG_WITNESS_FLAG, index->GetBlockHash())};
31+ std::vector<CInv> invs{CInv(MSG_BLOCK | MSG_WITNESS_FLAG, index.GetBlockHash())};
32 // Mark block as in-flight unless it already is
33- if (!MarkBlockAsInFlight(id, index->GetBlockHash(), index)) return false;
34+ if (!MarkBlockAsInFlight(id, index.GetBlockHash(), &index)) return false;
35 bool success = m_connman.ForNode(id, [this, invs](CNode* pnode) {
36 const CNetMsgMaker msgMaker(pnode->GetCommonVersion());
37 this->m_connman.PushMessage(pnode, msgMaker.Make(NetMsgType::GETDATA, invs));
38@@ -1276,12 +1276,12 @@ bool PeerManagerImpl::FetchBlock(NodeId id, const CBlockIndex* index)
39 });
40 if (success) {
41 LogPrint(BCLog::NET, "Requesting block %s from peer=%d\n",
42- index->GetBlockHash().ToString(), id);
43+ index.GetBlockHash().ToString(), id);
44 } else {
45 // Remove block from in-flight
46- MarkBlockAsReceived(index->GetBlockHash());
47+ MarkBlockAsReceived(index.GetBlockHash());
48 LogPrint(BCLog::NET, "Failed to request block %s from peer=%d\n",
49- index->GetBlockHash().ToString(), id);
50+ index.GetBlockHash().ToString(), id);
51 }
52 return success;
53 }
54diff --git a/src/net_processing.h b/src/net_processing.h
55index 577c925562..659504397f 100644
56--- a/src/net_processing.h
57+++ b/src/net_processing.h
58@@ -43,7 +43,7 @@ public:
59 virtual ~PeerManager() { }
60
61 /** Attempt to manually fetch block from a given node. */
62- virtual bool FetchBlock(const NodeId nodeid, const CBlockIndex* pindex) = 0;
63+ virtual bool FetchBlock(const NodeId nodeid, const CBlockIndex& pindex) = 0;
64
65 /** Get statistics from node state */
66 virtual bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const = 0;
67diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp
68index aa6b216ab1..cb3f2f3abc 100644
69--- a/src/rpc/blockchain.cpp
70+++ b/src/rpc/blockchain.cpp
71@@ -746,7 +746,7 @@ static RPCHelpMan getblockfrompeer()
72 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
73 }
74
75- if (!peerman.FetchBlock(nodeid, pblockindex)) {
76+ if (!peerman.FetchBlock(nodeid, *pblockindex)) {
77 throw JSONRPCError(RPC_MISC_ERROR, "Failed to fetch block from peer");
78 }
79 return UniValue::VOBJ;