test: use local `CBlockIndex` in block read hash mismatch test to avoid data race
What changed, and why it matters
This is a test-only code change that fixes a flaky unit test. The test previously borrowed a real block index object from the main chain and modified it, which could race with other test threads. The patch creates a local copy of the index data instead, so the test no longer touches shared state. It is not a fix for a security vulnerability in Bitcoin Core itself.
No security action required. Treat as normal test-quality improvement. If backporting, include only for test stability, not for vulnerability remediation.
Security signals we found
data race in test code
shared mutable state in unit test
test-only fix, no production code change
Evidence from the diff
The commit modifies src/test/blockmanager_tests.cpp in the blockmanager_readblock_hash_mismatch test. Previously it grabbed the active chain tip pointer under the chainman mutex, then mutated phashBlock outside any lock. That mutation of a shared CBlockIndex created a potential data race. The new code copies only the needed fields (nStatus, nDataPos) into a stack-allocated CBlockIndex, sets phashBlock to a mismatched value, and passes that local object to ReadBlock. This removes the race and makes the test deterministic. No production consensus or networking code is changed.
Changed components
src/test/blockmanager_tests.cppblockmanager_readblock_hash_mismatch unit testInspect captured patch +10 / −4
diff --git a/src/test/blockmanager_tests.cpp b/src/test/blockmanager_tests.cpp
index d3b58143..f06665d3 100644
--- a/src/test/blockmanager_tests.cpp
+++ b/src/test/blockmanager_tests.cpp
@@ -139,12 +139,18 @@ BOOST_FIXTURE_TEST_CASE(blockmanager_block_data_availability, TestChain100Setup)
BOOST_FIXTURE_TEST_CASE(blockmanager_readblock_hash_mismatch, TestingSetup)
{
- CBlockIndex* fake_index{WITH_LOCK(m_node.chainman->GetMutex(), return m_node.chainman->ActiveChain().Tip())};
- fake_index->phashBlock = &uint256::ONE; // invalid block hash
+ CBlockIndex index;
+ {
+ LOCK(cs_main);
+ const auto tip{m_node.chainman->ActiveTip()};
+ index.nStatus = tip->nStatus;
+ index.nDataPos = tip->nDataPos;
+ index.phashBlock = &uint256::ONE; // mismatched block hash
+ }
ASSERT_DEBUG_LOG("GetHash() doesn't match index");
- CBlock dummy;
- BOOST_CHECK(!m_node.chainman->m_blockman.ReadBlock(dummy, *fake_index));
+ CBlock block;
+ BOOST_CHECK(!m_node.chainman->m_blockman.ReadBlock(block, index));
}
BOOST_AUTO_TEST_CASE(blockmanager_flush_block_file)
Why this scored 17/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.