fix(safe-math): fix #20 by implementing overflow-safe arithmetic across contracts#29
Merged
EmeditWeb merged 1 commit intoJun 18, 2026
Conversation
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.
Closes #20
PR Description
This PR resolves issue #20 by addressing arithmetic overflow and underflow risks in calculations across the StepFi contracts. Multiple contracts performing math on i128 values for loans, late fees, share calculations, and boosts previously had no explicit overflow checks, which posed a security risk where extremely large numbers could wrap or cause unhandled panics.
I replaced raw operators (+, -, *) on financial values with explicit checked methods inside a standardized safe_math utility module implemented in each contract. When an overflow or underflow occurs, the contract now returns a custom, typed error (e.g.,
OverfloworUnderflow) instead of panicking, which allows client applications and invoking contracts to handle the error properly.Changes Made
safe_math.rsutility modules to all 5 core contracts (and the vouching contract) to implement checked addition, subtraction, multiplication, and division oni128,u64, andu32variables.lib.rsanderrors.rsfiles forcreditline-contract,liquidity-pool-contract,reputation-contract,parameters-contract,vendor-registry-contract, andvouching-contractto integrate the safe math helper functions and custom error variants.OverflowandUnderflowvariants to all contract-specific error enums to replace unhandled VM panics with typed error responses.test_safe_math_boundariesunit test incontracts/creditline-contract/src/tests.rsverifying that overflow, underflow, and divide-by-zero occurrences on extreme boundary values return the correct typed errors.