Skip to content

Strict address(this).balance Equality (Force-Feed)

SAFE-0251 Logic / State Checked automatically by the scanner
1

What goes wrong

Logic relies on an exact this.balance value (== or an invariant that the balance only changes via the contract's own functions). Ether can be force-fed via selfdestruct or a coinbase payment, breaking the equality and locking or mis-routing funds.

2

The vulnerable pattern

proof of concept — how it is exploited
// require(address(this).balance == expected);
// attacker selfdestructs a contract sending 1 wei to this address
// -> balance != expected forever -> the function is bricked.
3

How to fix it

the pattern that is safe
// track deposited value in a storage variable; never assume this.balance
uint256 internal _accounted;   // update on deposit/withdraw
Never assume this.balance only changes via your code.
Check your own contract for this

The SaferICO scanner runs 201 detectors over your Solidity source, SAFE-0251 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