Skip to content

Unsafe Downcast (uint256 → smaller)

SAFE-0122 Arithmetic Checked automatically by the scanner
1

What goes wrong

A wide integer is cast to a narrower type (uint128/uint64/uint32) without SafeCast. Values above the target range are silently truncated, corrupting accounting — a large deposit can wrap to a tiny stored amount, or a timestamp can overflow a uint32.

2

The vulnerable pattern

proof of concept — how it is exploited
// uint128 stored = uint128(amount);  // amount = 2**128 + 5
// stored becomes 5. Contract thinks you deposited 5 wei
// but pool math elsewhere used the full amount -> imbalance.
3

How to fix it

the pattern that is safe
uint128 x = SafeCast.toUint128(amount);   // reverts on overflow
Use OpenZeppelin SafeCast for every narrowing cast.
Check your own contract for this

The SaferICO scanner runs 201 detectors over your Solidity source, SAFE-0122 among them. Paste an address or the source itself — a small per-scan fee, shown before you sign, or unlimited on any plan.

Run the scanner See how it is attacked Read the docs