Skip to content

fix: handle overflow in BTC final TLC expiry delta calculation#1145

Merged
quake merged 2 commits intonervosnetwork:developfrom
doitian:fix-977-cch-btc-expiry-overflow
Mar 1, 2026
Merged

fix: handle overflow in BTC final TLC expiry delta calculation#1145
quake merged 2 commits intonervosnetwork:developfrom
doitian:fix-977-cch-btc-expiry-overflow

Conversation

@doitian
Copy link
Member

@doitian doitian commented Feb 25, 2026

Problem

When btc_final_tlc_expiry_delta_blocks is set to a very large value, multiplying it by 600 * 1000 to convert to milliseconds can overflow, causing unexpected behavior.

Related issue: #977

Solution

  • Use checked_mul instead of plain multiplication to detect overflow.
  • Return a new CchError::ConfigError when overflow is detected, with a descriptive message indicating the problematic configuration value.
  • Add the ConfigError variant to CchError enum.

Changes

  • crates/fiber-lib/src/cch/actor.rs: Replace * with checked_mul and propagate an error on overflow.
  • crates/fiber-lib/src/cch/error.rs: Add ConfigError(String) variant to CchError.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes an overflow vulnerability in the CCH (Cross-Chain Hub) module that occurs when btc_final_tlc_expiry_delta_blocks is configured with a very large value. The overflow happened during the conversion from BTC blocks to milliseconds when multiplying by 600,000 (10 minutes per block * 1000 milliseconds per second).

Changes:

  • Added ConfigError variant to CchError enum for configuration-related errors
  • Replaced unsafe multiplication with checked_mul to detect overflow conditions
  • Returns descriptive error message when overflow is detected in receive_btc function

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
crates/fiber-lib/src/cch/error.rs Added ConfigError(String) variant to handle configuration errors including overflow
crates/fiber-lib/src/cch/actor.rs Replaced direct multiplication with checked_mul and overflow detection in BTC block to millisecond conversion

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Replace the magic number `600 * 1000` with a documented named constant
`BTC_BLOCK_TIME_MILLIS` (600,000 ms = 10 minutes per Bitcoin block)
for improved readability and maintainability.
@quake quake merged commit 04813f6 into nervosnetwork:develop Mar 1, 2026
36 checks passed
@doitian doitian deleted the fix-977-cch-btc-expiry-overflow branch March 3, 2026 06:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[cch] btc_final_tlc_expiry attempt to add with overflow

3 participants