fix(security): guard all share-conversion divisions against zero deno… - #195
Open
BABAT-CODE wants to merge 2 commits into
Open
fix(security): guard all share-conversion divisions against zero deno…#195BABAT-CODE wants to merge 2 commits into
BABAT-CODE wants to merge 2 commits into
Conversation
…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
|
@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! 🚀 |
Contributor
|
Implement the features correctly |
Contributor
Author
|
How please i am confused. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
…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
/ decisivein finalize_proposal with math::safe_div(); adduse common::mathimport; 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 MathErrorcircle/src/contract.rs: replace raw
/ total_weighted(u128) in trigger_payout weighted-share loop with checked_div().unwrap_or(0); the outertotal_weighted > 0guard 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 entirelycloses #113