SC09—Integer Overflow and Underflow
>Control Description
Integer overflow and underflow occur when arithmetic operations produce values outside the representable range of the operand type. In Solidity 0.8+, arithmetic is checked by default and reverts on overflow/underflow. However, explicit `unchecked` blocks, assembly, and custom libraries can disable these checks.
On non-EVM platforms (Move, Sui, Solana, Rust-based chains), default overflow semantics differ significantly — some wrap silently, some abort — and incorrect assumptions or flawed custom checks can lead to wrapped values, miscomputed balances, and broken invariants.
This affects all contract types performing arithmetic: DeFi protocols (pool invariants, balances, interest calculations, share calculations), NFTs (supply tracking, token IDs), bridges (amounts, sequence numbers), and any logic involving large or user-controlled numeric inputs.
**Key Focus Areas:**
- EVM/Solidity: use of `unchecked`, assembly, pre-0.8 codebases
- Non-EVM chains: Move, Sui, Aptos, Solana, and their default overflow semantics
- Multiplication and exponentiation: high risk of overflow with large operands
- Subtraction and decrement: underflow when subtrahend exceeds minuend
- Casting and type conversion: downcasting (e.g., uint256 to uint128)
>Prevention & Mitigation Strategies
- 1.Avoid unchecked arithmetic unless you have strong reasons and comprehensive tests proving safety.
- 2.Use explicit checks and custom errors for critical invariants.
- 3.Favor well-reviewed math libraries for fixed-point arithmetic, exponentiation, etc.
- 4.Upgrade to Solidity 0.8.0+ if using pre-0.8 versions, or implement SafeMath library.
- 5.On non-EVM environments (Move, Rust-based chains), understand the language's default overflow semantics and use safe arithmetic constructs.
- 6.Test with extreme value ranges (minimum and maximum values for all numeric types).
- 7.Implement fuzz tests that target edge cases near boundaries where overflow/underflow is likely.
- 8.Conduct formal verification of custom overflow checks.
>Attack Scenarios
#1Cetus Protocol (May 2025, $223M loss)
The Cetus Protocol on Sui was exploited via a flawed checked_shlw function in the shared integer-mate library. The overflow check used the wrong threshold (0xFFFFFFFFFFFFFFFF << 192 instead of 1 << 192), allowing values >= 2^192 to pass. In Move, left shift operations do not abort on overflow — they truncate silently. The flawed check caused get_delta_a to calculate that only 1 token was required to mint enormous liquidity. Attackers exploited this across multiple pools using flash swaps, draining approximately $223M.
>References
Ask AI
Configure your API key to use AI features.