Skip to content

Commit

Permalink
Merge #11337: Fix code constness in CBlockIndex::GetAncestor() overloads
Browse files Browse the repository at this point in the history
b4058ed Fix code constness in CBlockIndex::GetAncestor() overloads (Dan Raviv)

Pull request description:

  Make the non-const overload of `CBlockIndex::GetAncestor()` reuse the const overload implementation instead of the other way around. This way, the constness of the const overload implementation is guaranteed. The other way around, it was possible to implement the non-const overload in a way which mutates the object, and since that implementation would be called even for const objects (due to the reuse), we would get undefined behavior.

Tree-SHA512: 545a8639bc52502ea06dbd924e8fabec6274fa69b43e3b8966a7987ce4dae6fb2498f623730fde7ed0e47478941c7f8baa2e76a12018134ff7c14c0dfa25ba3a
  • Loading branch information
laanwj committed Dec 1, 2017
2 parents 13e31dd + b4058ed commit 0d7e0a3
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/chain.cpp
Expand Up @@ -80,12 +80,13 @@ int static inline GetSkipHeight(int height) {
return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
}

CBlockIndex* CBlockIndex::GetAncestor(int height)
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
{
if (height > nHeight || height < 0)
if (height > nHeight || height < 0) {
return nullptr;
}

CBlockIndex* pindexWalk = this;
const CBlockIndex* pindexWalk = this;
int heightWalk = nHeight;
while (heightWalk > height) {
int heightSkip = GetSkipHeight(heightWalk);
Expand All @@ -106,9 +107,9 @@ CBlockIndex* CBlockIndex::GetAncestor(int height)
return pindexWalk;
}

const CBlockIndex* CBlockIndex::GetAncestor(int height) const
CBlockIndex* CBlockIndex::GetAncestor(int height)
{
return const_cast<CBlockIndex*>(this)->GetAncestor(height);
return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
}

void CBlockIndex::BuildSkip()
Expand Down

0 comments on commit 0d7e0a3

Please sign in to comment.