Solidity theorytheory 0/50 · 0%
Security · medium

25. Reentrancy

The classic attack and checks-effects-interactions.

An external call hands control to another contract, which may call back into you before your state updates finish. Order every function as checks, then effects (state writes), then interactions (external calls), and add a reentrancy guard for anything touching balances.

uint256 amount = balance[msg.sender];   // checks
balance[msg.sender] = 0;                // effects
(bool ok,) = msg.sender.call{value: amount}(""); // interactions
require(ok);

Cross-function and read-only reentrancy exist too: a view function can be called mid-attack and return stale data.

Check your understanding

  1. 1. What does CEI stand for?

  2. 2. Why is a guard still useful with CEI?

  3. 3. What is read-only reentrancy?