Skip to content

Replace math operators with checked/strict alternatives in contract examples#406

Open
gabrielrondon wants to merge 1 commit into
stellar:mainfrom
gabrielrondon:fix/383-strict-math-operators
Open

Replace math operators with checked/strict alternatives in contract examples#406
gabrielrondon wants to merge 1 commit into
stellar:mainfrom
gabrielrondon:fix/383-strict-math-operators

Conversation

@gabrielrondon
Copy link
Copy Markdown

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 increments
  • custom_types — parameterized increment
  • alloc — accumulator loop
  • liquidity_pool — all pool arithmetic (deposit, swap, withdraw, mint, burn)
  • mint-lock — epoch calculations

Not changed (by design)

  • Test files — test arithmetic is fine with operators
  • privacy-pools — ZK/BLS math internals where checked ops would add noise
  • Fuzzing targets — arbitrary generation uses range arithmetic

Known limitations

N/A

…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
Copilot AI review requested due to automatic review settings April 8, 2026 10:30
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 with checked_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.

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.

2 participants