Merge bitcoin/bitcoin#34521: validation: fix UB in LoadChainTip

20ae9b98ea Extend functional test for setBlockIndexCandidates UB (marcofleon)
854a6d5a9a validation: fix UB in LoadChainTip (marcofleon)
9249e6089e validation: remove LoadChainTip call from ActivateSnapshot (marcofleon)

Pull request description:

  Addresses https://github.com/bitcoin/bitcoin/issues/34503. See this issue for more details as well.

  Fixes a bug where, under certain conditions, `setBlockIndexCandidates` had blocks in it that were worse than the tip. The block index candidate set uses `nSequenceId` as a sort key, so modifying this field while blocks are in the set results in undefined behavior. This PR populates `setBlockIndexCandidates` after the `nSequenceId` modifications, avoiding the UB.

ACKs for top commit:
  achow101:
    ACK 20ae9b98ea
  sedited:
    Re-ACK 20ae9b98ea
  sipa:
    Code review ACK 20ae9b98ea

Tree-SHA512: 121c170bb70fb6365089d578db63c811e7926e129d7206e569947f7a1f6c5ddc8d5f4937b80f1ba1b7d7daa42789b143ca5b56f154b7ab968a1cd55f925f378d
This commit is contained in:
Ava Chow
2026-03-06 08:22:42 -08:00
6 changed files with 76 additions and 43 deletions

View File

@@ -77,8 +77,13 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
LOCK(::cs_main);
c2.InitCoinsCache(1 << 23);
c2.CoinsTip().SetBestBlock(active_tip->GetBlockHash());
c2.setBlockIndexCandidates.insert(manager.m_blockman.LookupBlockIndex(active_tip->GetBlockHash()));
for (const auto& cs : manager.m_chainstates) {
cs->ClearBlockIndexCandidates();
}
c2.LoadChainTip();
for (const auto& cs : manager.m_chainstates) {
cs->PopulateBlockIndexCandidates();
}
}
BlockValidationState _;
BOOST_CHECK(c2.ActivateBestChain(_, nullptr));
@@ -493,6 +498,9 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
BOOST_CHECK(cs->setBlockIndexCandidates.empty());
}
chainman.LoadBlockIndex();
for (const auto& cs : chainman.m_chainstates) {
cs->PopulateBlockIndexCandidates();
}
};
// Ensure that without any assumed-valid BlockIndex entries, only the current tip is