SSSwap

First Flight #41
Beginner FriendlyRust
100 EXP
View results
Submission Details
Severity: medium
Valid

Unsafe Casting from `u128` to `u64` Without Overflow Checks

Description

In the liquidity_operations.rs file, values are cast from u128 to u64 without checking whether the value fits within the u64 range. This can lead to silent truncation and incorrect calculations, especially when large input values are used.

Infected Code

In liquidity_calculation:

let lp_amount_to_mint: u64 = lp_amount_to_mint_u128 as u64;

In calculate_token_b_provision_with_a_given:

let amount_b_to_deposit: u64 = amount_b_to_deposit_u128 as u64;

In remove_liquidity instruction:

let amount_b_to_return = amount_b_to_return_u128 as u64;
let amount_a_to_return: u64 = amount_a_to_return_u128 as u64;

Impact

  • A malicious or misconfigured input could trigger an overflow scenario that causes token misallocation, accounting errors, or unexpected program behavior.

  • In the worst case, truncated values could result in incorrect minting or burning of tokens, potentially leading to financial loss or denial of service.

Recommendation

Use .try_into() or .try_from() with proper error propagation instead of direct as casting. This ensures the operation fails safely if the value exceeds u64::MAX.

Fixed Code Snippet

Example fix for liquidity_calculation:

let lp_amount_to_mint: u64 = lp_amount_to_mint_u128
.try_into()
.map_err(|_| AmmError::Overflow)?;

Apply similar error-handled casting in all other instances of u128 -> u64 conversions.

Updates

Lead Judging Commences

0xtimefliez Lead Judge 5 days ago
Submission Judgement Published
Validated
Assigned finding tags:

Unsafe Casting

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.