clarinet icon indicating copy to clipboard operation
clarinet copied to clipboard

fix: add overflow/underflow protection to counter example contracts

Open emmanuelist opened this issue 1 month ago • 1 comments

Description

Fixes compiler warnings about "use of potentially unchecked data" in the counter example contracts by adding proper overflow and underflow protection.

Changes

  • Add pre-operation validation to prevent arithmetic overflow in increment function
  • Add pre-operation validation to prevent underflow in decrement function
  • Return error u1 on overflow attempt, u2 on underflow attempt
  • Add comprehensive tests to verify overflow/underflow protection

Testing

  • All existing tests pass
  • Added new test for underflow protection
  • Added new test for overflow protection (u128 max value)
  • No compiler warnings (clarinet check passes cleanly)

Impact

  • Provides safer example code for developers learning Clarity
  • Demonstrates best practices for arithmetic operations
  • Eliminates all warnings from the counter example contracts

emmanuelist avatar Nov 07 '25 12:11 emmanuelist

Since this is just an example, I'd like to keep it simple. What do you think?

hugo-stacks avatar Nov 10 '25 14:11 hugo-stacks