Skip to content

Instantly share code, notes, and snippets.

@aquabu
Last active May 22, 2018 20:44
Show Gist options
  • Save aquabu/30378c5375f388a28572dd18d58f787f to your computer and use it in GitHub Desktop.
Save aquabu/30378c5375f388a28572dd18d58f787f to your computer and use it in GitHub Desktop.
demonstrating uint overflow and underflow in solidity
/* demonstrating uint overflow and underflow in ethereum solidity
this is why you need guards like:
if (balances[_to] + _amount < balances[_to]) throw;
*/
contract C {
// (2**256 - 1) + 1 = 0
function overflow() returns (uint256 _overflow) {
uint256 max = 2**256 - 1;
return max + 1;
}
// 0 - 1 = 2**256 - 1
function underflow() returns (uint256 _underflow) {
uint256 min = 0;
return min - 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment