aboutsummaryrefslogtreecommitdiff
path: root/src/validation.cpp
diff options
context:
space:
mode:
authorAdam Jonas <jonas@chaincode.com>2019-10-21 13:17:22 -0400
committerAurèle Oulès <aurele@oules.com>2022-05-05 15:55:44 +0200
commit308dd2e93e92f4cac4e7d75478316af9bb2b77b8 (patch)
tree520c6a3e925de6de2ded46a439c5b09a528f374f /src/validation.cpp
parentb1c5991eebb916755be188f355ad36fe01a3f529 (diff)
Sanity assert GetAncestor() != nullptr where appropriate
Add sanity asserts for return value of `CBlockIndex::GetAncestor()` where appropriate. In validation.cpp `CheckSequenceLocks`, check the return value of `tip->GetAncestor(maxInputHeight)` stored into `lp->maxInputBlock`. If it ever returns `nullptr` because the ancestor isn't found, it's going to be a bad bug to keep going, since a `LockPoints` object with the `maxInputBlock` member set to `nullptr` signifies no relative lock time. In the other places, the added asserts would prevent accidental dereferencing of a null pointer which is undefined behavior. Co-Authored-By: Aurèle Oulès <aurele@oules.com> Co-Authored-By: danra <danra@users.noreply.github.com>
Diffstat (limited to 'src/validation.cpp')
-rw-r--r--src/validation.cpp14
1 files changed, 10 insertions, 4 deletions
diff --git a/src/validation.cpp b/src/validation.cpp
index 58686632f9..25c99159fd 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -246,7 +246,12 @@ bool CheckSequenceLocksAtTip(CBlockIndex* tip,
maxInputHeight = std::max(maxInputHeight, height);
}
}
- lp->maxInputBlock = tip->GetAncestor(maxInputHeight);
+ // tip->GetAncestor(maxInputHeight) should never return a nullptr
+ // because maxInputHeight is always less than the tip height.
+ // It would, however, be a bad bug to continue execution, since a
+ // LockPoints object with the maxInputBlock member set to nullptr
+ // signifies no relative lock time.
+ lp->maxInputBlock = Assert(tip->GetAncestor(maxInputHeight));
}
}
return EvaluateSequenceLocks(index, lockPair);
@@ -4077,10 +4082,11 @@ bool CChainState::ReplayBlocks()
// Roll forward from the forking point to the new tip.
int nForkHeight = pindexFork ? pindexFork->nHeight : 0;
for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) {
- const CBlockIndex* pindex = pindexNew->GetAncestor(nHeight);
- LogPrintf("Rolling forward %s (%i)\n", pindex->GetBlockHash().ToString(), nHeight);
+ const CBlockIndex& pindex{*Assert(pindexNew->GetAncestor(nHeight))};
+
+ LogPrintf("Rolling forward %s (%i)\n", pindex.GetBlockHash().ToString(), nHeight);
uiInterface.ShowProgress(_("Replaying blocks…").translated, (int) ((nHeight - nForkHeight) * 100.0 / (pindexNew->nHeight - nForkHeight)) , false);
- if (!RollforwardBlock(pindex, cache)) return false;
+ if (!RollforwardBlock(&pindex, cache)) return false;
}
cache.SetBestBlock(pindexNew->GetBlockHash());