Skip to content

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

Closed
CapThunder19 wants to merge 2 commits into
bitcoindevkit:masterfrom
CapThunder19:fix/lookahead-to-target-overflow
Closed

fix(chain): prevent overflow in KeychainTxOutIndex::lookahead_to_target#2253
CapThunder19 wants to merge 2 commits 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 thread crates/chain/src/indexer/keychain_txout.rs Outdated
Comment thread crates/chain/src/indexer/keychain_txout.rs Outdated
@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.

@CapThunder19

Copy link
Copy Markdown
Author

pls review it

@evanlinjin

Copy link
Copy Markdown
Member

#2265 supersedes this.

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

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants