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.
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.
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.
uint128(x), uint64(x), int256(x) and similar conversions truncate silently, even on 0.8. Use OpenZeppelin's SafeCast or check the bound first.
add, sub and mul inside assembly { } never revert. Each one needs its own overflow reasoning.
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.
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.
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.
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)
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)
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.
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.
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.
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.
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.
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.