The Ethereum token tutorial has a check for a uint256 overflow.

function transfer(address _to, uint256 _value) {
    /* Check if sender has balance and for overflows */
    if (balanceOf[msg.sender] < _value || balanceOf[_to] + _value < balanceOf[_to])

    /* Add and subtract new balances */
    balanceOf[msg.sender] -= _value;
    balanceOf[_to] += _value;

I haven't found references elsewhere to guarding against uint overflows or security flaws in contracts because of them. It seems like both would be common if this was an issue.

3 Answers 3


Yep. Here's an example of uint overflow and underflow.

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;

You can execute the code here: https://remix.ethereum.org/#version=soljson-latest.js&gist=30378c5375f388a28572dd18d58f787f

  • Breaking changes in solidity v0.8.0+, they revert overflows with a standard panic error, straight from the documentation... "Failing assertions and other internal checks like division by zero or arithmetic overflow do not use the invalid opcode but instead the revert opcode. More specifically, they will use error data equal to a function call to Panic(uint256) with an error code specific to the circumstances." May 12, 2021 at 5:29

Yes, overflow is possible, and be especially careful when using var for example:

In for (var i = 0; i < arrayName.length; i++) { ... }, the type of i will be uint8, because this is the smallest type that is required to hold the value 0. If the array has more than 255 elements, the loop will not terminate.

Using uint i (256 bits) avoids the problem.

Note: The EVM doesn't allow for infinite computation, so the loop will consume all gas and the transaction will be terminated, with fees still being paid to the miner.


Since solidity v0.8.0+ it is not possible to overflow or underflow uints anymore.

If you check @Noah Thorp's answer, you will see solidity will revert and throw an error.

Straight from the docs:

Arithmetic operations revert on underflow and overflow. You can use unchecked { ... } to use the previous wrapping behaviour.

This is now done automatically!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.