Skip to content

Commit 2736969

Browse files
laanwjPastaPastaPasta
authored andcommitted
Merge bitcoin#11337: Fix code constness in CBlockIndex::GetAncestor() overloads
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
1 parent 759f34a commit 2736969

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/chain.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,13 @@ int static inline GetSkipHeight(int height) {
8080
return (height & 1) ? InvertLowestOne(InvertLowestOne(height - 1)) + 1 : InvertLowestOne(height);
8181
}
8282

83-
CBlockIndex* CBlockIndex::GetAncestor(int height)
83+
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
8484
{
85-
if (height > nHeight || height < 0)
85+
if (height > nHeight || height < 0) {
8686
return nullptr;
87+
}
8788

88-
CBlockIndex* pindexWalk = this;
89+
const CBlockIndex* pindexWalk = this;
8990
int heightWalk = nHeight;
9091
while (heightWalk > height) {
9192
int heightSkip = GetSkipHeight(heightWalk);
@@ -106,9 +107,9 @@ CBlockIndex* CBlockIndex::GetAncestor(int height)
106107
return pindexWalk;
107108
}
108109

109-
const CBlockIndex* CBlockIndex::GetAncestor(int height) const
110+
CBlockIndex* CBlockIndex::GetAncestor(int height)
110111
{
111-
return const_cast<CBlockIndex*>(this)->GetAncestor(height);
112+
return const_cast<CBlockIndex*>(static_cast<const CBlockIndex*>(this)->GetAncestor(height));
112113
}
113114

114115
void CBlockIndex::BuildSkip()

0 commit comments

Comments
 (0)