I read that whenever you are performing arithmetic operations in a function of a smart contract, you should use SafeMath library. What is the reason for using SafeMath library? Also, what are the advantages and disadvantages of this library during arithmetic operations?
2 Answers
Safemath library is useful for prevent integer rage overflow or underflow after integer operations. Its very important because it will impact user data or global ERC20 or any token value. Resultant your token will returns unexpected results. You can't update existing contract.
If you write extra statement in ethereum that consumes gas for steps. i.e it consumes eth. When compare to advantage's this is negotiable.
Advantage: if there is a 256-bit overflow in the computation, then the operation will be aborted with an exception, and all preceding changes in state (global) variables will be reverted.
Disadvantage: cost slightly more gas for each operation.