Skip to content

Fix stable_math::compute_invariant panics on a zero balance - #171

Merged
johngrantuk merged 6 commits into
rust-fix-4from
rust-fix-5
Aug 12, 2026
Merged

Fix stable_math::compute_invariant panics on a zero balance#171
johngrantuk merged 6 commits into
rust-fix-4from
rust-fix-5

Conversation

@johngrantuk

Copy link
Copy Markdown
Member

5. stable_math::compute_invariant panics on a zero balance

for balance in balances {
    d_p = (d_p * invariant) / (balance * U256::from(num_tokens));
}

Any zero balance divides by zero. Reachable for a pool with three or more tokens where one has been
drained: a caller screening only the two tokens being swapped still hands the whole balance array to
this function.

Suggested fix: return PoolError when any balance is zero — the invariant is undefined there.

@johngrantuk
johngrantuk merged commit 9573c5f into rust-fix-4 Aug 12, 2026
3 checks passed
@johngrantuk
johngrantuk deleted the rust-fix-5 branch August 12, 2026 08:26
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.

1 participant