Change CChain::Next() to take reference

To minimize chance of erroneous nullptr dereference, `CChain::Next()`
is changed to take a reference instead of a pointer.
Call sites have been adapted. Notably, NextSyncBlock() now checks
the FindFork() result before calling into Next(), because
the fork lookup may return null.
This commit is contained in:
optout
2026-01-29 12:01:23 +01:00
parent fe2d6e25e0
commit 20b58e281a
7 changed files with 21 additions and 21 deletions

View File

@@ -4215,10 +4215,10 @@ void PeerManagerImpl::ProcessMessage(Peer& peer, CNode& pfrom, const std::string
// Send the rest of the chain
if (pindex)
pindex = m_chainman.ActiveChain().Next(pindex);
pindex = m_chainman.ActiveChain().Next(*pindex);
int nLimit = 500;
LogDebug(BCLog::NET, "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom.GetId());
for (; pindex; pindex = m_chainman.ActiveChain().Next(pindex))
for (; pindex; pindex = m_chainman.ActiveChain().Next(*pindex))
{
if (pindex->GetBlockHash() == hashStop)
{
@@ -4354,14 +4354,14 @@ void PeerManagerImpl::ProcessMessage(Peer& peer, CNode& pfrom, const std::string
// Find the last block the caller has in the main chain
pindex = m_chainman.ActiveChainstate().FindForkInGlobalIndex(locator);
if (pindex)
pindex = m_chainman.ActiveChain().Next(pindex);
pindex = m_chainman.ActiveChain().Next(*pindex);
}
// we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
std::vector<CBlock> vHeaders;
int nLimit = m_opts.max_headers_result;
LogDebug(BCLog::NET, "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom.GetId());
for (; pindex; pindex = m_chainman.ActiveChain().Next(pindex))
for (; pindex; pindex = m_chainman.ActiveChain().Next(*pindex))
{
vHeaders.emplace_back(pindex->GetBlockHeader());
if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)