Integer Bugs
Integer overflow and underflow were common vulnerabilities before Solidity 0.8.0. Understanding these bugs is still important for auditing older contracts and unchecked blocks — and for arithmetic that no compiler checks at all.
Authorised Targets Only
Overflow/Underflow Examples
On solc 0.8.34
the compiler inserts a check on every arithmetic operation, so the wrap only happens inside an
unchecked block
or in a contract still compiled with a pre-0.8.0 version. Both samples below are written for 0.8.34; the first
uses
unchecked to
show the pre-0.8.0 behaviour deliberately.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
contract WrapDemo {
// 0.8+ would revert here; unchecked restores the old wrap
function overflow() public pure returns (uint8) {
unchecked {
uint8 x = 255;
return x + 1; // 0
}
}
function underflow() public pure returns (uint8) {
unchecked {
uint8 y = 0;
return y - 1; // 255
}
}
}
contract VulnerableToken {
mapping(address => uint256) public balances;
// Real-world impact: on solc <0.8.0, an amount larger than the
// caller's balance wraps and credits the recipient a huge balance.
// On 0.8+ the subtraction reverts - unless it is wrapped in
// unchecked, which is exactly the mistake to look for in review.
function transfer(address to, uint256 amount) public {
unchecked {
balances[msg.sender] -= amount;
balances[to] += amount;
}
}
}None of this is history. Cetus Protocol, a concentrated-liquidity AMM on Sui, lost roughly $223M in May 2025 to a single missing overflow check in a shared liquidity-math helper — the arithmetic was in Move rather than Solidity, and the check was hand-written instead of compiler-inserted. The review question is the same on any VM: which operations can wrap, and what guarantees they cannot.
Mitigation
The mitigation is the compiler, not a library. Since 0.8.0 checked arithmetic is the default, so a new contract
needs nothing beyond a current
solc version —
and the review question moves to why any
unchecked block
exists at all.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.34;
contract SafeToken {
mapping(address => uint256) public balances;
// 0.8+ arithmetic is checked: this reverts on an insufficient
// balance instead of crediting the recipient out of thin air.
function transfer(address to, uint256 amount) public {
balances[msg.sender] -= amount;
balances[to] += amount;
}
}
// HISTORICAL ONLY. OpenZeppelin v5 removed SafeMath entirely, so the
// import below does not exist in current releases. Pre-0.8.0 contracts
// needed it because the compiler did not check anything:
//
// using SafeMath for uint256; // OZ 4.x and earlier
// balances[msg.sender] = balances[msg.sender].sub(amount);
// balances[to] = balances[to].add(amount);
//
// Do not add SafeMath to a new project - upgrade the compiler instead.
// Auditing checklist:
// 1. Record the exact solc version per contract (and per library)
// 2. List every unchecked block and justify each one
// 3. Audit custom math libraries, whatever the compiler or language
// 4. Check multiplication before division for precision loss
// 5. Validate user-supplied ranges before they reach arithmeticRelated Topics
Reentrancy
The other bug class that survives a compiler upgrade, because no version of solc checks call ordering.
Access Control
Missing modifiers, which often let an attacker reach the arithmetic they could not otherwise call.
Auditing
Where the solc version and unchecked-block inventory fit into a real audit.