Integer Overflow in Programming
Published: 2021-04-25 18:02:51

###**What is an Integer Overflow?**
An integer overflow is a type of an arithmetic overflow error(calculation run by a computer but is unable to store the answer correctly) where the result of an integer operation does not fit within the allocated memory space. *Integer overflow* occurs when an arithmetic operation attempts to create a numeric value that is outside of the range that can be represented with a given number of bits – either larger than the maximum or lower than the minimum representable value.

Integer values are usually allocated a certain number of bits in memory, for example:
- 8 bit integer can store has a range of 0 -255
- 16 bit integer has a range of 0 - 65,535
- 32 bit integer has a range of 0 - 4,294,967,295 for unsigned integer or −2,147,483,648 to 2,147,483,647 signed integer.

As defined above, if you input an integer operation greater than the range specified, then you get an integer overflow. Unfortunately, different programming languages handle this differently and worse still is most languages do not raise any sort of error when this happens.

###**Dangers of Integer Overflows**
Integer overflows can cause various issues in the system, here are some of them: 
- Major one is faulty records / data corruption. A common behaviour when an integer overflow occurs is it resets itself and starts again. this may lead to financial and bad records being getting recorded.
 *How you ask?* - Well, since most languages do not raise an error, they simply perform a modulo operation, wraparound, or truncation, or they have other undefined behavior. In most cases they result to a 0. If this, say was someones bank account, you can see how bad this can turn out to be.

- If an integer overflow happens when you calculate the length of a buffer, you may end up with a buffer overflow. A buffer overflow can let an attacker gain shell access and attempt to execute other hacks. Basically letting them into the system. 

- Can cause infinite loops as well. If the system keeps resetting the value to zero or truncating it, etc this might result in an infinite loop since the value will inevitably never get to the end. this may result in overloading the computer and causing crashes etc.

###**How can this be prevented?**
The bad news is that most are hard to debug and discover since they do not throw an error but with this knowledge, you can take precautions to make sure this does not happen. Here are some of the ways:
- You can examine the operands before the operation or examine the result and predict if the values have the potential to overflow.
- Depending on the language you are using, some have libraries in place that can help check out for this types of errors. A good example is SafeMath.sol library in solidity language.
- You can use logic tests to check if the result is good. Say you have a computation like: 
`a=b+c` then you can do a check like:
`b=a-c` and `c=a-b` thus if it is reset to 0 this should produce an error. 

###**References and good reads as well:**
- https://consensys.github.io/smart-contract-best-practices/known_attacks/#integer-overflow-and-underflow
- https://www.securecoding.com/blog/integer-overflow-attack-and-prevention/
- https://blockgeeks.com/guides/underflow-attacks-smart-contracts/
- https://www.sciencedirect.com/topics/computer-science/integer-overflow