Integer overflow and underflow
Overflow or underflow occurs when the value assigned to a variable exceeds the limit allowed for that data type. This is very common for the integer data type in solidity and has to be carefully verified during an assignment.
This recipe explains the situations in which integer overflow/underflow can occur and the ways to avoid them.
Getting ready
This recipe is all about solidity-based smart contracts. The Remix IDE (https://remix.ethereum.org) can help you quickly test and deploy a contract.
Also, you can use any Ethereum client (geth
, parity
, and so on) and the solc
compiler to run this contract.
How to do it...
- Consider the following basic token contract:
pragma solidity ^0.4.23; contract TokenContract { mapping (address => uint) balances; event Transfer( address indexed _from, address indexed _to, uint256 _value ); function sendToken(address receiver, uint amount) public returns(bool) { require...