index: add explicit early exit in NextSyncBlock() when the input is the chain tip
What changed, and why it matters
This is a small code cleanup in Bitcoin Core's index synchronization logic. It adds an explicit early return when the code has already reached the latest block in the chain, separating that case from the more complex 'block is no longer in the main chain' reorganization handling. The change is primarily about making the code's intent clearer and avoiding unnecessary work, not about fixing a known security bug.
Treat as a routine code-quality/refactoring commit. No urgent security action required. Reviewers may want to confirm that the early return is functionally equivalent for all callers and does not alter reorg handling behavior.
Security signals we found
Defensive code-path separation between 'synced to tip' and 'reorganization' cases
Avoids unnecessary `FindFork()` call on the chain tip
No memory safety, cryptographic, or network-visible change identified
No explicit bug fix or vulnerability disclosure in commit message
Evidence from the diff
In src/index/base.cpp, NextSyncBlock() previously called chain.Next(pindex_prev) and, if it returned null, fell through to a reorg-handling path that calls chain.FindFork(pindex_prev). The patch adds an explicit check: if pindex_prev == chain.Tip(), return nullptr immediately before the reorg logic. This avoids invoking FindFork() on the chain tip and makes the ‘already synced’ case distinct from the ‘block left the main chain’ case. The functional behavior appears unchanged for valid inputs; the change is defensive/correctness-oriented.
Changed components
src/index/base.cppBaseIndex sync logicNextSyncBlock helperInspect captured patch +6 / −2
diff --git a/src/index/base.cpp b/src/index/base.cpp
index fba2f4f6..2cb3a2a0 100644
--- a/src/index/base.cpp
+++ b/src/index/base.cpp
@@ -155,11 +155,15 @@ static const CBlockIndex* NextSyncBlock(const CBlockIndex* pindex_prev, CChain&
return chain.Genesis();
}
- const CBlockIndex* pindex = chain.Next(pindex_prev);
- if (pindex) {
+ if (const auto* pindex{chain.Next(pindex_prev)}) {
return pindex;
}
+ // If there is no next block, we might be synced
+ if (pindex_prev == chain.Tip()) {
+ return nullptr;
+ }
+
// Since block is not in the chain, return the next block in the chain AFTER the last common ancestor.
// Caller will be responsible for rewinding back to the common ancestor.
return chain.Next(chain.FindFork(pindex_prev));
Why this scored 16/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.