Integer over/under -flow in Solidity

Welcome to the thread about integer over/under -flow Installation. Here you can discuss everything about the issue of underflow and overflow and the Safemath library.

1 Like

In the text it is said that underflow and overflow appear when you have you maximum value ( i.e. 2^256 - 1) or minimum value (i.e. 0 ) AND increment/decrement it by 1. I assume even for any increment/decrement by x , you have also underflow and overflow ?

4 Likes

Yes that is correct! Good point.

1 Like

the link to the Safemath library in the course reading isn’t working. here they are:

docs: https://openzeppelin.org/api/docs/math_SafeMath.html
code: https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/math/SafeMath.sol

1 Like

Thank you. I have updated the text in teachable.

1 Like

finally i understood the meaning of under/overflow . thanks Filip

if we have to use safeMath , do we have to import the file of the library safeMath the same way we import a regular file?

Yes, that’s correct.

hi

I dont really understand what we mean by integer overflows. If we for example add a calculation that is too large this will result in an integer overflow? To formulate differently, what are usual cases where integer overflows have happened in programs/Smart contracts?

Yes, if you add onto an integer so that it becomes very very large (2³¹-1). If you continue to add to that number it will “restart” from -2³¹. This is a very common mistake in both solidity contracts and in the real world.

You can look at some examples here from solidity: https://medium.com/@yenthanh/prevent-integer-overflow-in-ethereum-smart-contracts-a7c84c30de66

Here you can find some real world examples, where even planes and rockets have crashed because of integer over/under flow. https://en.wikipedia.org/wiki/Integer_overflow#Examples

3 Likes

Thank you very much filip, this helps a lot :slight_smile:

1 Like