CBlockIndex: ensure phashBlock is not nullptr before dereferencing

and remove a now-redundant assert preceding a GetBlockHash() caller.

This protects against UB here, and in case of failure (which would
indicate a consensus bug), the debug log will print

bitcoind: chain.h:265: uint256 CBlockIndex::GetBlockHash() const: Assertion `phashBlock != nullptr' failed.
Aborted

instead of

Segmentation fault
This commit is contained in:
Jon Atack 2022-07-16 13:22:46 +02:00
parent 02ede4f1fd
commit 14aeece462
2 changed files with 1 additions and 1 deletions

View file

@ -263,6 +263,7 @@ public:
uint256 GetBlockHash() const uint256 GetBlockHash() const
{ {
assert(phashBlock != nullptr);
return *phashBlock; return *phashBlock;
} }

View file

@ -2263,7 +2263,6 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
m_blockman.m_dirty_blockindex.insert(pindex); m_blockman.m_dirty_blockindex.insert(pindex);
} }
assert(pindex->phashBlock);
// add this block to the view's block chain // add this block to the view's block chain
view.SetBestBlock(pindex->GetBlockHash()); view.SetBestBlock(pindex->GetBlockHash());