Solidity Bug Hunt Hard +35 XP
Reentrancy Vulnerability
This withdraw function is vulnerable to reentrancy.
SolidityFind the bug
1
function withdraw() external {2
uint amount = balances[msg.sender];3
(bool ok,) = msg.sender.call{value: amount}("");4
balances[msg.sender] = 0;5
} The bug is on line 4
Select the correct fix
Show answer and explanation
Answer: Both A and C fix it
CEI pattern: Check-Effects-Interactions. Update state (balances=0) before external calls. Or use a nonReentrant modifier (ReentrancyGuard).
Hint: Update state before making external calls.
What this challenge tests
Bug Hunt in Solidity: reentrancy, security, cei.