Arithmetic Vulnerability

Integer Overflow
& Underflow Guide

Integer overflow and underflow vulnerabilities occur when arithmetic operations exceed variable limits. Learn how they work and how to prevent them in your smart contracts.

Practical audit tutorial: checking a contract for integer overflow in 6 steps
Work through these in order. Steps 2–5 matter even on Solidity 0.8+.

1. Check the compiler version

Find pragma solidity. Below 0.8.0, every +, - and * wraps silently. Every operation on balances, supply or prices needs SafeMath (or an explicit bounds check), not just some of them.

2. Audit every unchecked block

On 0.8+, search for unchecked {. For each one, write down why the math can't overflow, such as a loop counter bounded by an array length. If you can't prove it, remove the block.

3. Find explicit downcasts

uint128(x), uint64(x), int256(x) and similar conversions truncate silently, even on 0.8. Use OpenZeppelin's SafeCast or check the bound first.

4. Read the inline assembly

add, sub and mul inside assembly { } never revert. Each one needs its own overflow reasoning.

5. Check shifts and multiply-then-divide

Shifts (<<) aren't overflow-checked. And a * b / c can overflow on a * b even when the final result fits; use Math.mulDiv for price and share math.

6. Fuzz the arithmetic

Write Foundry fuzz tests that push inputs toward the edges with bound(x, 0, type(uint128).max), and assert that totals and balances still add up. Then run a scanner as a second pair of eyes; see the Detection tab for what ours covers.

What are Integer Overflow & Underflow?

Integer overflow occurs when an arithmetic operation results in a value larger than the maximum value that can be stored in the variable. The value "wraps around" to the minimum value. Underflow is the opposite - when a value becomes smaller than the minimum and wraps to the maximum.

How Integer Overflow Works

Overflow Example (uint256)

Maximum value: 2²⁵⁶ - 1 = 115,792,089,237,316,195,423,570,985,008,687,907,853,269,984,665,640,564,039,457,584,007,913,129,639,935
Maximum + 1 = 0 (wraps around)

Underflow Example (uint256)

Minimum value: 0
0 - 1 = 115,792,089,237,316,195,423,570,985,008,687,907,853,269,984,665,640,564,039,457,584,007,913,129,639,935 (wraps around)

Common Attack Scenarios

Token Balance Manipulation

  • • Underflow in transfer functions
  • • Creating tokens from nothing
  • • Balance corruption
  • • Supply manipulation

Financial Calculations

  • • Price calculation errors
  • • Interest rate overflows
  • • Fee calculation issues
  • • Reward distribution bugs
Solidity Version Differences

Solidity < 0.8.0

  • • No built-in overflow protection
  • • Silent overflow/underflow
  • • Requires SafeMath library
  • • Manual checks needed

Solidity ≥ 0.8.0

  • • Built-in overflow protection
  • • Automatic revert on overflow
  • • SafeMath no longer needed
  • • Unchecked blocks available

Frequently Asked Questions

What is integer overflow in Solidity smart contracts?+

Integer overflow occurs when an arithmetic operation produces a value larger than the maximum the variable type can hold, causing it to wrap around to zero (or minimum). In Solidity < 0.8.0, `uint8` overflowing 255 wraps to 0. Attackers exploit this to mint unlimited tokens or bypass balance checks.

Does Solidity 0.8 prevent integer overflow automatically?+

Yes — Solidity 0.8.0 introduced built-in overflow checks that revert the transaction on overflow or underflow. However, contracts using `unchecked {}` blocks, assembly, or third-party libraries compiled under older versions remain vulnerable. Always verify which Solidity version your contract uses.

What is the difference between overflow and underflow in Solidity?+

Overflow is when a value exceeds the maximum (e.g., uint256 max + 1 = 0). Underflow is when a value goes below the minimum (e.g., uint256(0) - 1 = 2^256-1). Both were exploited before Solidity 0.8. In April 2018 an overflow in BeautyChain's batchTransfer let an attacker give two addresses 2^255 BEC each, out of nothing.

What isn't protected by Solidity 0.8's overflow checks?+

Five things still wrap or truncate silently on 0.8+: arithmetic inside unchecked { } blocks, explicit downcasts such as uint128(x) (the high bits are simply dropped), arithmetic in inline assembly, bit shifts, and any library or contract compiled with an older pragma. Also watch multiplication before division: a * b can overflow and revert even when a * b / c would fit.

Has integer overflow caused real DeFi exploits?+

Yes. Before Solidity 0.8, overflow bugs were common. The BEC token hack (2018) used a multiplication overflow to mint 2^255 tokens to each of two addresses. The SMT token and PoWH Coin had similar issues. Even post-0.8, unchecked blocks, downcasts and assembly still create exposure.

How can I check my smart contract for integer overflow?+

Follow the six-step audit on this page. SmartContractAuditor.ai's pattern engine flags arithmetic in contracts compiled below Solidity 0.8 without SafeMath, rating it high when the math uses caller-supplied values. The AI review (trial and paid plans) reads the whole contract, including unchecked blocks, downcasts and assembly.

Check Your Contract for Overflow Vulnerabilities

Scan your contract to flag unchecked arithmetic on pre-0.8 Solidity in seconds. On the trial, the AI review also reads unchecked blocks, downcasts and assembly.

Free arithmetic vulnerability scan • Instant results • SafeMath recommendations