Skip to content

Cross-Function Reentrancy

SAFE-0002 Reentrancy Checked automatically by the scanner
1

What goes wrong

State shared across multiple functions is left inconsistent during an external call. The attacker re-enters through a DIFFERENT function that reads the not-yet-updated shared state, bypassing a guard that only protects the original function.

2

How to fix it

the pattern that is safe
function withdraw() external nonReentrant {
    uint256 bal = balances[msg.sender];
    balances[msg.sender] = 0;                          // update before call
    (bool ok,) = msg.sender.call{value: bal}("");
    require(ok);
}
function transfer(address to, uint256 amt) external nonReentrant {
    balances[msg.sender] -= amt;
    balances[to] += amt;
}
Apply the guard or CEI to the full set of functions sharing state.
Check your own contract for this

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