
Integer Overflow in Solidity: What It Is, Real Examples, and How to Prevent It (2026)
An integer overflow vulnerability occurs when arithmetic operations exceed the maximum value a data type can hold — causing the value to wrap around to zero or a small number. Before Solidity 0.8.0, this was a critical security flaw that allowed attackers to manipulate balances and bypass checks. The BEC Token hack in 2018 exploited integer overflow to generate $900 million worth of tokens from nothing. Use the free Blockhertz AI Smart Contract Auditor to check your contracts for overflow vulnerabilities in under 60 seconds.
https://www.youtube.com/embed/vIDQ7nMppDIWhat is Integer Overflow in Solidity?
Every integer type in Solidity has a fixed range. A uint256 can hold values from 0 to 2²⁵⁶-1. When an arithmetic operation exceeds this maximum, the value wraps around — like an odometer rolling over from 99999 to 00000. Before Solidity 0.8.0, this wrapping happened silently with no error. Attackers exploited this behavior to manipulate token balances, bypass transfer checks, and drain contracts.
Overflow Example
// In Solidity < 0.8.0: uint256 max = type(uint256).max; uint256 result = max + 1; // result = 0 (wraps around!)
Underflow Example
// In Solidity < 0.8.0: uint256 zero = 0; uint256 result = zero - 1; // result = 115792089237316195... // (wraps to maximum value!)
How Did the $900 Million BEC Token Hack Happen?
In April 2018, an attacker exploited an integer overflow vulnerability in the BeautyChain (BEC) token contract. The vulnerable batch transfer function allowed users to send tokens to multiple addresses. The attacker passed a carefully crafted value that caused an integer overflow in the amount calculation — generating an astronomically large token balance from nothing. The attacker then dumped these tokens on exchanges, causing the BEC token price to crash to near zero and wiping out an estimated $900 million in market value.
The Vulnerable Code
// VULNERABLE — BEC Token style
// Solidity ^0.4.16
function batchTransfer(
address[] receivers,
uint256 value
) public returns (bool) {
uint cnt = receivers.length;
// ❌ OVERFLOW HERE:
uint256 amount = uint256(cnt) * value;
require(value > 0 &&
balances[msg.sender] >= amount);
balances[msg.sender] =
balances[msg.sender].sub(amount);
for (uint i = 0; i < cnt; i++) {
balances[receivers[i]] =
balances[receivers[i]].add(value);
Transfer(msg.sender, receivers[i], value);
}
return true;
}
How Do You Prevent Integer Overflow in Solidity?
Prevent integer overflow by upgrading to Solidity 0.8.0 or higher which has built-in protection, using OpenZeppelin SafeMath for legacy contracts, and being careful with unchecked blocks and type casting.
Does Solidity 0.8 Automatically Prevent Integer Overflow?
Yes — Solidity 0.8.0 introduced automatic overflow and underflow protection. Every arithmetic operation now reverts automatically if it would overflow or underflow — no additional code required. Simply upgrading to Solidity 0.8.0+ eliminates integer overflow vulnerabilities entirely for standard arithmetic.
// SECURE — Solidity ^0.8.0
pragma solidity ^0.8.20;
contract SecureToken {
mapping(address => uint256) public balances;
function transfer(address to, uint256 amount) external {
// ✅ Automatically reverts if underflow
balances[msg.sender] -= amount;
balances[to] += amount;
}
}
What is SafeMath and When Should You Use It?
SafeMath is an OpenZeppelin library for Solidity versions below 0.8.0 that prevents integer overflow and underflow by reverting on unsafe arithmetic operations. It is no longer needed in Solidity 0.8.0 and above. Only use SafeMath when maintaining legacy contracts that cannot be upgraded to Solidity 0.8+.
// SECURE — Legacy Solidity with SafeMath
pragma solidity ^0.6.0;
import "@openzeppelin/contracts/math/SafeMath.sol";
contract SecureToken {
using SafeMath for uint256;
mapping(address => uint256) public balances;
function batchTransfer(
address[] memory receivers,
uint256 value
) public returns (bool) {
uint256 cnt = receivers.length;
// ✅ SafeMath reverts on overflow
uint256 amount = cnt.mul(value);
require(value > 0 && balances[msg.sender] >= amount);
balances[msg.sender] = balances[msg.sender].sub(amount);
for (uint i = 0; i < cnt; i++) {
balances[receivers[i]] = balances[receivers[i]].add(value);
}
return true;
}
}
When is it Safe to Use unchecked Blocks in Solidity?
Use unchecked blocks only when overflow is mathematically impossible — such as loop counters already bounded by array length checks. Never use unchecked for token balances, amounts, or any value that could be influenced by user input.
// ✅ SAFE use of unchecked
function processArray(uint256[] memory data) external {
for (uint256 i = 0; i < data.length;) {
// process data[i]
unchecked {
++i; // safe: i < data.length guarantees no overflow
}
}
}
// ❌ UNSAFE use of unchecked
function dangerousAdd(uint256 a, uint256 b) external pure returns (uint256) {
unchecked {
return a + b; // can overflow!
}
}
Integer Overflow Checklist
Check Status
Using Solidity 0.8.0 or higher
✅ Required
SafeMath used for legacy Solidity
✅ Required if < 0.8.0
unchecked blocks only where safe
✅ Required
Multiplication before division
✅ Recommended
Array length × value checked
✅ Required
No casting from large to small types
✅ Required
Can Type Casting Cause Overflow in Solidity?
Yes — casting from a larger integer type to a smaller one can cause overflow even in Solidity 0.8.0+. Use OpenZeppelin's SafeCast library to safely downcast between integer types with automatic revert on overflow.
// ❌ RISKY — explicit cast truncates value uint256 large = 257; uint8 small = uint8(large); // small = 1 (256 truncated!) // ✅ SAFE — use OpenZeppelin SafeCast import "@openzeppelin/contracts/utils/math/SafeCast.sol"; uint256 large = 257; uint8 small = SafeCast.toUint8(large); // Reverts if value doesn't fit in uint8
Why Does Multiplication Order Matter in Solidity?
Always multiply before dividing to avoid precision loss. Dividing first truncates the result to an integer before multiplication amplifies it — this can create exploitable rounding behavior in financial calculations.
// ❌ WRONG order — precision loss uint256 result = (a / b) * c; // ✅ CORRECT order — multiply first uint256 result = (a * c) / b;
How Do You Detect Integer Overflow in Smart Contracts?
The Blockhertz AI Smart Contract Auditor automatically detects integer overflow vulnerabilities in your Solidity, Rust, Move, or Vyper contracts in under 60 seconds.
- Go to blockhertz.com/tools/ai-auditor
- Paste your smart contract
- Click Audit Contract
- Get a full security report with risk score and fix recommendations
Free to start — no signup required: blockhertz.com/tools/ai-auditor
Summary
- Integer overflow = arithmetic wraps around when exceeding max value
- BEC Token hack = $900M exploited via overflow in 2018
- Solidity 0.8.0+ = automatic overflow protection built in
- Legacy contracts = use OpenZeppelin SafeMath
- unchecked blocks = only when overflow is mathematically impossible
- Type casting = use SafeCast for safe downcasting
References
- BatchOverflow Bug in BEC Token — PeckShield (2018)
- Checked/Unchecked Arithmetic — Solidity Documentation
- SafeCast — OpenZeppelin Documentation
- SWC-101: Integer Overflow — Smart Contract Weakness Registry
- Insecure Arithmetic — Consensys Best Practices
Related Blockhertz Tools
Most common vulnerability explained
Automatically detect integer overflow and 10+ other vulnerabilities in your smart contracts at blockhertz.com/tools/ai-auditor — free, no signup required.
Frequently Asked Questions
What is integer overflow and underflow in Solidity?
Integer overflow occurs when an arithmetic result exceeds a type's maximum value and wraps to its minimum (e.g., uint8 at 255 + 1 = 0). Integer underflow is the reverse — going below the minimum wraps to the maximum (e.g., uint8 at 0 - 1 = 255). In DeFi contracts this is catastrophic: an overflowed token balance can appear astronomically large, letting attackers transfer tokens they don't own, or an underflowed fee calculation can result in zero fees charged.
How does SafeMath prevent integer overflow in Solidity?
SafeMath is an OpenZeppelin library (used with Solidity < 0.8) that wraps every arithmetic operation with an explicit check and reverts on overflow. For example, SafeMath.add(a, b) checks that a + b >= a before returning the result — if the check fails, the transaction reverts. In Solidity 0.8+, this behavior is built-in at the compiler level, making SafeMath unnecessary (though using unchecked blocks re-introduces the risk).
What Solidity types are vulnerable to overflow?
All unsigned integer types (uint8 through uint256) and signed integer types (int8 through int256) can overflow or underflow. Smaller types overflow more easily: uint8 overflows at 255, uint16 at 65,535. uint256 is hardest to overflow in practice, but downcasting from uint256 to a smaller type (e.g., uint128(largeNumber)) silently truncates the high bits — a common source of subtle overflow bugs even in Solidity 0.8.
What is the unchecked block in Solidity 0.8 and when is it safe to use?
The unchecked { } block disables the compiler's automatic overflow checks for the code inside it, restoring pre-0.8 behavior for a gas saving. It is safe only when you have mathematically proven no overflow is possible — for example, loop counters in a for (uint i = 0; i < arr.length; ++i) where i cannot exceed arr.length which is itself bounded. Never use unchecked on user-supplied values, token amounts, or any arithmetic where the range cannot be proven at compile time.
What is a real-world integer overflow exploit example?
The BatchOverflow attack (2018) exploited an integer overflow in multiple ERC-20 token contracts. The attacker called a batch transfer function with a carefully crafted value parameter that overflowed when multiplied by the number of recipients, producing a near-zero result that passed the balance check — while the actual transfer credited recipients with enormous token amounts. This single vulnerability drained or inflated balances across over 50 tokens on BNB Chain, leading to widespread trading halts.
How do auditors check for integer overflow in smart contracts?
Auditors combine automated and manual techniques: (1) run Slither with the taint-analysis detector to flag arithmetic on user-controlled inputs; (2) check every explicit type downcast (uint128(x), uint8(y)) for silent truncation; (3) review every unchecked block manually to verify the no-overflow invariant holds; (4) use fuzzing tools like Echidna to generate inputs that trigger arithmetic edge cases; (5) verify Solidity compiler version — projects still on 0.7.x need SafeMath on every operation.
Frequently Asked Questions
What is integer overflow in Solidity?
+
How does Solidity 0.8 prevent integer overflow?
+
What is SafeMath in Solidity?
+
Resource Hub
Explore our Smart Contract Security Hub →
Free AI auditor, vulnerability guides and security research
Related Articles
AI Won't Protect Your Smart Contract — Your Audit Loop Will
Most developers run one AI audit, fix the findings, and deploy. That single pass is exactly why 51.11% of deployed contracts remain exploitable. The loop is the mechanism — AI is just the tool inside it.
Read article →Smart Contract SecurityFlash Loan Attack Surface: How It Works and How to Defend Your Smart Contract
Flash loan attacks have drained over $500 million from DeFi protocols in the last four years. Unlike traditional exploits that require the attacker to hold capital upfront, flash loans let anyone bor...
Read article →Smart Contract SecurityBlock Timestamp Manipulation in Solidity — How Miners Exploit block.timestamp (2026)
block.timestamp looks harmless — until a miner shifts it by 15 seconds and drains your lottery, your vesting schedule, or your governance vote. Here is exactly how timestamp manipulation works in Solidity and how to fix it before you deploy.
Read article →Related Blockhertz Tools
Explore Blockhertz
Views
67
Read Time
6 min read
Likes
2
Published
Aug 12, 2026
SIGNAL THREAD
NO SIGNALS YET — BE FIRST TO TRANSMIT
Muhammad Asif
Founder, Blockhertz
Founder, Blockhertz. Founded the company in 2018 to build AI-powered tools for Web3 teams — smart contract auditing, architecture generation, gas optimization, and RWA tokenization platforms. Serving clients worldwide.