We have seen how many attacks on smart contracts are often due to bugs contained within the source code. To prevent the occurrence of these bugs, it is thus appropriate to use specialized library functions that help the developer in the safe implementation of the most common functions. One of these libraries is the SafeMath library of the OpenZeppelin package (available at https://github.com/OpenZeppelin/openzeppelin-solidity).
By using the SafeMath library, it is possible to prevent bugs such as integer overflows and underflows.
In the following example, we show an excerpt of the implementation of the add() function offered by the SafeMath library:
pragma solidity ^0.4.24;
library SafeMath {
...
/**
* @dev Function to add two numbers
*/
function add(uint256 a, uint256 b)
internal pure returns (uint256 c) {
c = a + b;
assert(c >= a);
...