- Anyone can withdraw all money - The biggest problem
- Missing semicolon - Won't compile
- Empty attack() function - Suspicious
The audited-VulnerablePiggyBank.sol fixes these issues:
- Only owner can withdraw - Added
onlyOwnermodifier - Fixed syntax - Added missing semicolon
- Removed attack() - Removed unnecessary function
- Better transfer method - Used
callinstead oftransfer
// OLD - BROKEN
function withdraw() public {
payable(msg.sender).transfer(address(this).balance);
}
// NEW - SECURE
function withdraw() public onlyOwner {
(bool success, ) = payable(owner).call{value: address(this).balance}("");
require(success, "Transfer failed");
}- ❌ Old contract: Anyone can steal all funds
- ✅ New contract: Only owner can withdraw funds