A reentrancy attack is a serious vulnerability where a function calls an external contract, allowing that contract to make repeated calls back before execution completes.
The DAO hack in 2016 exploited a reentrancy vulnerability, resulting in approximately $60 million USD in losses.
// VULNERABLE: State updated after external callfunction withdraw(uint256 amount) public { require(balances[msg.sender] >= amount); // External call BEFORE state update (bool success, ) = msg.sender.call{value: amount}(""); require(success); // State update happens too late balances[msg.sender] -= amount;}
Use reentrancy guards (e.g., OpenZeppelin ReentrancyGuard) on sensitive functions
Use .call{value: ...}("") for ETH transfers (not transfer()/send())
Always check the boolean return value from .call and handle failures explicitly
transfer() and send() are deprecated due to their hardcoded 2300 gas limit, which can fail after gas cost changes (EIP-1884). Use .call{value: ...}("") instead.
Assistant
Responses are generated using AI and may contain mistakes.