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

@@ -132,7 +132,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
for (const CBlockIndex* block_index = m_node.chainman->ActiveChain().Genesis();
block_index != nullptr;
block_index = m_node.chainman->ActiveChain().Next(block_index)) {
block_index = m_node.chainman->ActiveChain().Next(*block_index)) {
BOOST_CHECK(!filter_index.LookupFilter(block_index, filter));
BOOST_CHECK(!filter_index.LookupFilterHeader(block_index, filter_header));
BOOST_CHECK(!filter_index.LookupFilterRange(block_index->nHeight, block_index, filters));
@@ -152,7 +152,7 @@ BOOST_FIXTURE_TEST_CASE(blockfilter_index_initial_sync, BuildChainTestingSetup)
const CBlockIndex* block_index;
for (block_index = m_node.chainman->ActiveChain().Genesis();
block_index != nullptr;
block_index = m_node.chainman->ActiveChain().Next(block_index)) {
block_index = m_node.chainman->ActiveChain().Next(*block_index)) {
CheckFilterLookups(filter_index, block_index, last_header, m_node.chainman->m_blockman);
}
}

View File

@@ -76,10 +76,9 @@ BOOST_AUTO_TEST_CASE(basic_tests)
BOOST_CHECK(!chain_0.Contains(genesis));
// Call with non-tip & tip blocks
BOOST_CHECK_EQUAL(chain_2.Next(&genesis), &bi1);
BOOST_CHECK_EQUAL(chain_2.Next(&bi1), nullptr);
BOOST_CHECK_EQUAL(chain_0.Next(&genesis), nullptr);
// BOOST_CHECK_EQUAL(chain_0.Next(nullptr), nullptr); // fail with memory access violation
BOOST_CHECK_EQUAL(chain_2.Next(genesis), &bi1);
BOOST_CHECK_EQUAL(chain_2.Next(bi1), nullptr);
BOOST_CHECK_EQUAL(chain_0.Next(genesis), nullptr);
BOOST_CHECK_EQUAL(chain_2.Genesis(), &genesis);
BOOST_CHECK_EQUAL(chain_0.Genesis(), nullptr);