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

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.

1 participant