Created
October 1, 2020 16:01
-
-
Save Mayur1496/cddb660b9a37be55f0679c923654bf98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.19; | |
contract Reentrance { | |
mapping (address => uint) userBalance; | |
address public owner; | |
function Reentrance() public{ | |
owner = msg.sender; | |
} | |
function getBalance(address u) constant returns(uint){ | |
return userBalance[u]; | |
} | |
function addToBalance() payable{ | |
userBalance[msg.sender] += msg.value; | |
} | |
function withdrawBalance(){ | |
// send userBalance[msg.sender] ethers to msg.sender | |
// if mgs.sender is a contract, it will call its fallback function | |
if( ! (msg.sender.call.value(userBalance[msg.sender])() ) ){ | |
throw; | |
} | |
userBalance[msg.sender] = 0; | |
} | |
function withdrawBalance_fixed(){ | |
// to protect against re-entrancy, the state variable | |
// has to be change before the call | |
uint amount = userBalance[msg.sender]; | |
userBalance[msg.sender] = 0; | |
if( ! (msg.sender.call.value(amount)() ) ){ | |
throw; | |
} | |
} | |
function withdrawBalance_fixed_2(){ | |
// send() and transfer() are safe against reentrancy | |
// they do not transfer the remaining gas | |
// and they give just enough gas to execute few instructions | |
// in the fallback function (no further call possible) | |
msg.sender.transfer(userBalance[msg.sender]); | |
userBalance[msg.sender] = 0; | |
} | |
function get_money(){ | |
suicide(owner); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment