---
title: "Integer overflow, underflow and unsafe casts"
url: https://dontgetrekt.today/dev/integer-overflow
section: "Is the math and the design right?"
owasp: SC09:2026
---

# Integer overflow, underflow and unsafe casts

A number wraps around: a balance under zero becomes huge, or a large value cut to fewer bits becomes small. Solidity 0.8 checks most math, but not unchecked blocks, casts or assembly.

## How the attack works

1. Before Solidity 0.8, `+`, `-` and `*` wrapped silently. Many old contracts and forks still use 0.6 or 0.7.
2. In 0.8, math inside `unchecked { }` and in inline assembly still wraps.
3. A cast such as `uint128(x)` cuts the high bits with no error.
4. The attacker gives a value that wraps, so a check passes or a balance becomes very large.

## Vulnerable code

```solidity
// Solidity 0.7: no checks on math
function transfer(address to, uint256 amt) external {
    require(balance[msg.sender] - amt >= 0); // always true: uint underflows
    balance[msg.sender] -= amt;
    balance[to] += amt;
}

// Solidity 0.8: a cast cuts the high bits with no error
uint128 stored = uint128(amount);
```

## Fixed code

```solidity
// Solidity ^0.8.20: checked math by default
function transfer(address to, uint256 amt) external {
    require(balance[msg.sender] >= amt, "balance");
    balance[msg.sender] -= amt;
    balance[to] += amt;
}

import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
uint128 stored = SafeCast.toUint128(amount); // reverts if too large
```

## Review checklist

- Use Solidity 0.8 or later. If you must use an older version, use SafeMath on every operation.
- Use `unchecked` only where you can prove that the value cannot wrap. Write the proof in a comment.
- Use `SafeCast` for every downcast.
- Review all inline assembly math by hand.

## How to test for it

- Fuzz with values near `type(uint256).max` and near zero.
- Search the code for `unchecked`, `assembly` and `uint128(`, `uint64(`, `int256(` casts.

## Real exploits (13)

- 2018-04-22 Beauty Chain (BEC) ($900.0M): Beauty Chain's BEC token lost a reported $900 million of token value when an integer overflow in batchTransfer() let the attacker mint huge balances. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2018-04/BEC_exp.sol)
- 2025-05-22 Cetus ($223.0M): Cetus, a Sui DEX, lost about $223M (of which $162M was frozen by validators) because a flawed overflow check in the shared integer-mate math library let 1 token mint huge liquidity. [rekt.news](https://rekt.news/cetus-rekt)
- 2018-04-24 SmartMesh ($140.0M): SmartMesh's SMT token lost about $140M (reported) because transferProxy() did unchecked arithmetic on _value + _feeSmt, which overflowed and let the attacker mint a huge balance. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2018-04/SmartMesh_exp.sol)
- 2026-01-08 Truebit ($26.2M): Truebit lost about $26.2M (8,535 ETH) when an unchecked addition in getPurchasePrice() of a 5-year-old Solidity 0.5.3 bonding-curve contract overflowed, letting the attacker mint TRU for almost nothing and burn it for ETH. [rekt.news](https://rekt.news/truebit-rekt) [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2026-01/Truebit_exp.sol)
- 2025-11-30 Yearn (yETH stableswap pool) ($9.0M): Yearn's legacy yETH stableswap pool lost about $9M when an unchecked arithmetic underflow in its Newton-Raphson supply solver let an attacker mint a huge amount of yETH from a dust deposit. [rekt.news](https://rekt.news/yearn-rekt3)
- 2025-03-05 1inch (Fusion v1 resolvers / TrustedVolumes) ($5.0M): 1inch's deprecated Fusion v1 settlement code let an attacker drain about $5M from resolver TrustedVolumes by setting interactionLength to -512, causing an integer underflow and calldata corruption in Yul code. [rekt.news](https://rekt.news/1inch-rekt)
- 2026-09-03 Notional Finance (V1) ($1.7M): Notional Finance V1 lost about $1.73M (DAI and USDC) because an ERC-1155 fCash transfer minted unbacked claims and a uint128 cash ladder overflowed. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2026-09/NotionalFinance_exp.sol)
- 2022-03-20 Umbrella Network ($700K): Umbrella Network lost about $700k because StakingRewards._withdraw() subtracted balances without overflow checks, so an attacker with no stake could withdraw LP tokens. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2022-03/Umbrella_exp.sol)
- 2023-03-15 Poolz ($390K): Poolz lost about $390K on BNB Chain because CreateMassPools in the LockedDeal contract summed token amounts with an overflow, so the attacker could create a huge pool and withdraw the contract's whole token balance. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2023-03/poolz_exp.sol)
- 2025-03-28 Alkimiya (SilicaPools) ($96K): Alkimiya SilicaPools lost about $95.5K (1.14 WBTC) because an unsafe uint256-to-uint128 cast of shares in collateralizedMint let the attacker mint huge positions with almost no collateral. [PoC](https://github.com/SunWeb3Sec/DeFiHackLabs/blob/main/src/test/2025-03/Alkimiya_io_exp.sol)

## References

- [OWASP SC09:2026](https://scs.owasp.org/sctop10/SC09-IntegerOverflowUnderflow/)
- [SWC-101: Integer overflow and underflow](https://swcregistry.io/docs/SWC-101/)
- [Solidity docs: Checked or unchecked arithmetic](https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic)
