Skip to content

Commit 39497d1

Browse files
author
MarcoFalke
committed
Merge #15283: log: Fix UB with bench on genesis block
ec30a79 Fix UB with bench on genesis block (Gregory Sanders) Pull request description: During the loading of the genesis block, the bench print lines in ConnectTip divide by zero due to early return in ConnectBlock. ACKs for top commit: practicalswift: ACK ec30a79 sipa: utACK ec30a79 promag: ACK ec30a79, `nBlocksTotal` is only used in logging. Tree-SHA512: b3bdbb58d10d002a2293d7f99196b227ed9f4ca8c6cd08981e95cc964be47efed98b91fad276ee6da5cf7e6684610998ace7ce9bace172dd6c51c386d985b83c
2 parents ad04f0d + ec30a79 commit 39497d1

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/validation.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,8 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
19271927
uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->GetBlockHash();
19281928
assert(hashPrevBlock == view.GetBestBlock());
19291929

1930+
nBlocksTotal++;
1931+
19301932
// Special case for the genesis block, skipping connection of its transactions
19311933
// (its coinbase is unspendable)
19321934
if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
@@ -1935,8 +1937,6 @@ bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state,
19351937
return true;
19361938
}
19371939

1938-
nBlocksTotal++;
1939-
19401940
bool fScriptChecks = true;
19411941
if (!hashAssumeValid.IsNull()) {
19421942
// We've been configured with the hash of a block which has been externally verified to have a valid history.
@@ -2598,6 +2598,7 @@ bool CChainState::ConnectTip(BlockValidationState& state, const CChainParams& ch
25982598
return error("%s: ConnectBlock %s failed, %s", __func__, pindexNew->GetBlockHash().ToString(), state.ToString());
25992599
}
26002600
nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
2601+
assert(nBlocksTotal > 0);
26012602
LogPrint(BCLog::BENCH, " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime3 - nTime2) * MILLI, nTimeConnectTotal * MICRO, nTimeConnectTotal * MILLI / nBlocksTotal);
26022603
bool flushed = view.Flush();
26032604
assert(flushed);

0 commit comments

Comments
 (0)