fix(chain): prevent overflow in KeychainTxOutIndex::lookahead_to_target - #2253
Open
CapThunder19 wants to merge 1 commit into
Open
fix(chain): prevent overflow in KeychainTxOutIndex::lookahead_to_target#2253CapThunder19 wants to merge 1 commit into
KeychainTxOutIndex::lookahead_to_target#2253CapThunder19 wants to merge 1 commit into
Conversation
evanlinjin
requested changes
Aug 17, 2026
evanlinjin
left a comment
Member
There was a problem hiding this comment.
Thanks for the PR. Concept ACK.
Comment on lines
+551
to
+561
| /// Computes how many additional lookahead scripts are needed to cover `target_index` | ||
| /// (inclusive), given the next index that would be derived (`next_index`). | ||
| /// | ||
| /// Returns `None` if `target_index` is already covered (i.e. `target_index < next_index`). | ||
| fn lookahead_delta(target_index: u32, next_index: u32) -> Option<u32> { | ||
| target_index | ||
| .saturating_add(1) | ||
| .checked_sub(next_index) | ||
| .filter(|&index| index > 0) | ||
| } | ||
|
|
Member
There was a problem hiding this comment.
Please inline this. A separate method here is not helpful.
Comment on lines
+1156
to
+1173
| /// `lookahead_delta` must not panic (via overflow) when `target_index` is `u32::MAX`, and | ||
| /// must saturate rather than silently returning a wrong/empty result. | ||
| #[test] | ||
| fn lookahead_delta_does_not_overflow_at_u32_max() { | ||
| // Fresh keychain (next_index = 0): delta should saturate to u32::MAX, not overflow. | ||
| assert_eq!( | ||
| KeychainTxOutIndex::<i32>::lookahead_delta(u32::MAX, 0), | ||
| Some(u32::MAX) | ||
| ); | ||
| // Target already covered by next_index: no lookahead needed. | ||
| assert_eq!( | ||
| KeychainTxOutIndex::<i32>::lookahead_delta(u32::MAX, u32::MAX), | ||
| None | ||
| ); | ||
| // Normal, non-boundary case still behaves as before. | ||
| assert_eq!(KeychainTxOutIndex::<i32>::lookahead_delta(10, 5), Some(6)); | ||
| } | ||
|
|
Member
There was a problem hiding this comment.
This unit test does not add any value. Please remove.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2253 +/- ##
==========================================
+ Coverage 78.36% 78.40% +0.03%
==========================================
Files 30 30
Lines 5945 5955 +10
Branches 281 281
==========================================
+ Hits 4659 4669 +10
Misses 1210 1210
Partials 76 76
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
lookahead_to_targetcomputedtarget_index + 1with uncheckedaddition. With
target_index == u32::MAX, this panics in debug buildsand silently no-ops in release builds (wraps to 0, so the lookahead
request is quietly ignored).
Fixed by extracting the delta calculation into a
lookahead_deltahelper using
saturating_addinstead of+, matching the patternalready used elsewhere in this file.
Testing
Added
lookahead_delta_does_not_overflow_at_u32_max, a unit test on thehelper directly. (Not an integration test calling
lookahead_to_targetend-to-end with
u32::MAX, since that would try to derive ~2^31 realkeys on a fresh index.)
cargo test --features miniscript --libcargo test --features miniscript --test test_keychain_txout_indexcargo fmt --check -p bdk_chaincargo clippy -p bdk_chain --features miniscript --lib -- -D warnings#2251