Replace math operators with checked/strict alternatives in contract examples - #406
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
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.
|
Friendly ping. This has been open since April with only the Copilot review, and it still merges cleanly into main. The motivation stands: these examples are the first thing new contract developers copy, and bare arithmetic in them normalizes overflow-prone patterns in real contracts. Happy to rebase or narrow the scope (for example, only the counter-style examples) if that makes review easier. |
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