Replace math operators with checked/strict alternatives in contract examples#406
Open
gabrielrondon wants to merge 1 commit into
Open
Replace math operators with checked/strict alternatives in contract examples#406gabrielrondon wants to merge 1 commit into
gabrielrondon wants to merge 1 commit into
Conversation
…xamples Replace `+`, `-`, `*`, `/` operators with `checked_add`, `checked_sub`, `checked_mul`, `checked_div` in contract source files to prevent silent integer overflow/underflow in on-chain execution. Closes stellar#383
Contributor
There was a problem hiding this comment.
Pull request overview
This PR replaces bare arithmetic operators with their checked equivalents across 12 contract example files to encourage developers to explicitly handle overflow/underflow cases when using these examples as starting points for their own contracts.
Changes:
- Replace
+=operators withchecked_add(...).expect("overflow")in simple counter increments across 8 files - Replace arithmetic operators (
+,-,*,/) with checked variants in complex calculations (liquidity pool swaps, minting, withdrawals) - Add explicit type annotations for variables used in checked operations for improved clarity
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| increment/src/lib.rs | Replace simple counter increment |
| increment_with_pause/src/lib.rs | Replace simple counter increment |
| increment_with_fuzz/src/lib.rs | Replace simple counter increment |
| events/src/lib.rs | Replace simple counter increment |
| errors/src/lib.rs | Replace simple counter increment |
| bls_signature/src/lib.rs | Replace simple counter increment |
| auth/src/lib.rs | Replace simple counter increment with variable amount |
| other_custom_types/src/lib.rs | Replace simple counter increment |
| custom_types/src/lib.rs | Replace parameterized counter increment |
| alloc/src/lib.rs | Add type annotation to accumulator variable and replace sum increment with checked_add |
| mint-lock/src/lib.rs | Replace division and addition operations in epoch calculations with checked variants |
| liquidity_pool/src/lib.rs | Replace all arithmetic operators in deposit, swap, withdraw, and mint operations with checked variants |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
What
Replace bare arithmetic operators (
+,-,*,/,+=) with their checked equivalents (checked_add,checked_sub,checked_mul,checked_div) across 12 contract example source files.Why
Encourage developers to think about overflow failure cases when copying these examples as starting points for their own contracts. Raw arithmetic operators silently wrap or panic on overflow — checked functions make overflow handling explicit.
Ref: #383
Changed examples
increment,events,errors,auth,bls_signature,increment_with_pause,increment_with_fuzz,other_custom_types— simple counter incrementscustom_types— parameterized incrementalloc— accumulator loopliquidity_pool— all pool arithmetic (deposit, swap, withdraw, mint, burn)mint-lock— epoch calculationsNot changed (by design)
privacy-pools— ZK/BLS math internals where checked ops would add noiseKnown limitations
N/A