Last active
May 22, 2018 20:44
-
-
Save aquabu/30378c5375f388a28572dd18d58f787f to your computer and use it in GitHub Desktop.
demonstrating uint overflow and underflow in solidity
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
/* 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