Skip to content

fix(security): guard all share-conversion divisions against zero deno… - #195

Open
BABAT-CODE wants to merge 2 commits into
cocor-tech:masterfrom
BABAT-CODE:fix/zero-division-share-math
Open

fix(security): guard all share-conversion divisions against zero deno…#195
BABAT-CODE wants to merge 2 commits into
cocor-tech:masterfrom
BABAT-CODE:fix/zero-division-share-math

Conversation

@BABAT-CODE

Copy link
Copy Markdown
Contributor

…minators

Fixes a zero-division vulnerability reported in common/src/math.rs where dividing by total_shares/total_assets without a zero check would cause an on-chain panic (contract abort) if the pool hits an empty-share edge case.

Changes:

  • common/src/math.rs: add shares_to_assets() and assets_to_shares() helpers with explicit zero-checks on total_shares / total_assets denominators respectively; add 20 unit tests covering zero denominator, zero numerator, normal, proportional, and overflow cases for both helpers plus safe_div, calculate_percentage, and apply_fee

  • governance/src/contract.rs: replace raw / decisive in finalize_proposal with math::safe_div(); add use common::math import; extract intermediate votes_for_scaled binding for readability; the decisive > 0 short-circuit was already present but relying on evaluation order alone is fragile — safe_div makes the invariant explicit and returns a typed MathError

  • circle/src/contract.rs: replace raw / total_weighted (u128) in trigger_payout weighted-share loop with checked_div().unwrap_or(0); the outer total_weighted > 0 guard prevents a zero denominator in practice but the raw division was an unguarded foot-gun — checked_div makes intent explicit and eliminates the panic path entirely

closes #113

…minators

Fixes a zero-division vulnerability reported in common/src/math.rs where
dividing by total_shares/total_assets without a zero check would cause an
on-chain panic (contract abort) if the pool hits an empty-share edge case.

Changes:
- common/src/math.rs: add shares_to_assets() and assets_to_shares() helpers
  with explicit zero-checks on total_shares / total_assets denominators
  respectively; add 20 unit tests covering zero denominator, zero numerator,
  normal, proportional, and overflow cases for both helpers plus safe_div,
  calculate_percentage, and apply_fee

- governance/src/contract.rs: replace raw `/ decisive` in finalize_proposal
  with math::safe_div(); add `use common::math` import; extract intermediate
  votes_for_scaled binding for readability; the decisive > 0 short-circuit
  was already present but relying on evaluation order alone is fragile —
  safe_div makes the invariant explicit and returns a typed MathError

- circle/src/contract.rs: replace raw `/ total_weighted` (u128) in
  trigger_payout weighted-share loop with checked_div().unwrap_or(0); the
  outer `total_weighted > 0` guard prevents a zero denominator in practice
  but the raw division was an unguarded foot-gun — checked_div makes intent
  explicit and eliminates the panic path entirely
@drips-wave

drips-wave Bot commented Aug 3, 2026

Copy link
Copy Markdown

@BABAT-CODE Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@oraimoitel

Copy link
Copy Markdown
Contributor

Implement the features correctly

@BABAT-CODE

Copy link
Copy Markdown
Contributor Author

How please i am confused.

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.

fix(contract): prevent division by zero in share conversion when total_shares is zero

2 participants