Skip to content

[bug] liquidity-pool: withdraw shares * total_liquidity multiplication has no overflow guard — large pools can overflow i128 #725

Description

@gboigwe

Summary

withdraw computes the token amount using:

let amount = (shares * pool.total_liquidity) / total_shares;

Both shares and pool.total_liquidity are i128. If a provider holds a large share position in a pool with high total liquidity, the intermediate multiplication shares * total_liquidity can overflow i128::MAX ≈ 1.7 × 10^38. The result would be silently wrong, producing an incorrect (possibly negative) withdrawal amount.

Location

contracts/liquidity-pool/src/lib.rs, withdraw

let amount = (shares * pool.total_liquidity) / total_shares; // ❌ no overflow guard

Fix

let amount = shares
    .checked_mul(pool.total_liquidity)
    .expect("share redemption calculation overflows i128")
    / total_shares;

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions