Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions fixed solidity
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

/**
* @title FixedContract
* @dev 修复了 Ethernaut 原关卡中的漏洞,防止重入攻击
*/
contract FixedContract {
mapping(address => uint256) public balances;
bool private locked;

modifier noReentrant() {
require(!locked, "Reentrant call");
locked = true;
_;
locked = false;
}

function deposit() public payable {
balances[msg.sender] += msg.value;
}

function withdraw(uint256 _amount) public noReentrant {
require(balances[msg.sender] >= _amount, "Insufficient balance");
balances[msg.sender] -= _amount;
(bool success, ) = msg.sender.call{value: _amount}("");
require(success, "Withdraw failed");
}
}
Loading