Skip to content

fix(chain): prevent overflow in KeychainTxOutIndex::lookahead_to_target - #2253

Open
CapThunder19 wants to merge 1 commit into
bitcoindevkit:masterfrom
CapThunder19:fix/lookahead-to-target-overflow
Open

fix(chain): prevent overflow in KeychainTxOutIndex::lookahead_to_target#2253
CapThunder19 wants to merge 1 commit into
bitcoindevkit:masterfrom
CapThunder19:fix/lookahead-to-target-overflow

Conversation

@CapThunder19

@CapThunder19 CapThunder19 commented Aug 4, 2026

Copy link
Copy Markdown

Description

lookahead_to_target computed target_index + 1 with unchecked
addition. With target_index == u32::MAX, this panics in debug builds
and 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_delta
helper using saturating_add instead of +, matching the pattern
already used elsewhere in this file.

Testing

Added lookahead_delta_does_not_overflow_at_u32_max, a unit test on the
helper directly. (Not an integration test calling lookahead_to_target
end-to-end with u32::MAX, since that would try to derive ~2^31 real
keys on a fresh index.)

  • cargo test --features miniscript --lib
  • cargo test --features miniscript --test test_keychain_txout_index
  • cargo fmt --check -p bdk_chain
  • cargo clippy -p bdk_chain --features miniscript --lib -- -D warnings

#2251

@evanlinjin evanlinjin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unit test does not add any value. Please remove.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@codecov

codecov Bot commented Aug 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.85714% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.40%. Comparing base (337e9d6) to head (3e76f04).
⚠️ Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
crates/chain/src/indexer/keychain_txout.rs 92.85% 0 Missing and 1 partial ⚠️
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              
Flag Coverage Δ
rust 78.40% <92.85%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants