Skip to content

Instantly share code, notes, and snippets.

@asselstine
Created September 12, 2018 02:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asselstine/c3632e4a04d154d1da3b384e71db1952 to your computer and use it in GitHub Desktop.
Save asselstine/c3632e4a04d154d1da3b384e71db1952 to your computer and use it in GitHub Desktop.
A Simple Example of Integer underflow
pragma solidity ^0.4.4;
/*
This contract is vulnerable to an attacker exploting
integer underflow. When you subtract from unsigned integers
at their lowest value (0), they cycle to their highest value.
Similarly, adding 1 to a max value unsigned integer will
cycle it to zero.
*/
contract IntUnderflow {
uint32 public reputation;
function IntUnderflow() public {
reputation = 0;
}
function upvote() external {
reputation += 1;
}
// if reputation is zero, this will cause reputation
// to underflow to 2^32 -1
function downvote() external {
reputation -= 1;
}
function getReputation() public constant returns (uint32) {
return reputation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment