1

I've been told SafeMath doesn't matter.

Does SafeMath slow things down, cost more ETH, or does it add an extra level of stability? Will SafeMath be unsupported one day?

contract SafeMath {
function safeAdd(uint a, uint b) public pure returns (uint c) {
    c = a + b;
    require(c >= a);
}
function safeSub(uint a, uint b) public pure returns (uint c) {
    require(b <= a); c = a - b; } function safeMul(uint a, uint b) public pure returns (uint c) { c = a * b; require(a == 0 || c / a == b); } function safeDiv(uint a, uint b) public pure returns (uint c) { require(b > 0);
    c = a / b;
}

Also is using compiler 0.5.0 still okay? Is it outdated?

Thank you.

3 Answers 3

2

Solidity had a historical issue with arithmetic overflows and underflows, that's why having SafeMath enabled was a necessity back in the day. SafeMath has been made obsolete starting from Solidity v0.8.0, as internal checking for arithmetic operations was added by default. I would suggest to always use the latest version of Solidity as possible.

2

SafeMath is used to protect your contract against math errors, such as overflow (adding uint above the max uint, for instance). SafeMath will revert the transaction if bad math happens in a transaction. It may very slightly increase the gas used for the transaction, but the benefit of safety is generally worth it.

SafeMath is no longer needed if you use Solidity version 0.8.0 and onward. It has been integrated by default in the compiler.

If you do want to use SafeMath with an older compiler, I suggest that you instead rely on a well-known library such as the one from openzeppelin and not build your own SafeMath module.

It is best practice to use the most recent version of the Solidity compiler, since security issues are fixed with compiler updates. It is not wrong to use an older version, but you should have reason to do so.

0

The top comment of the updated safemath library now says:

CAUTION This version of SafeMath should only be used with Solidity 0.8 or later, because it relies on the compiler's built in overflow checks.

So now the code, for example, of the add function is actually :

function add(uint256 a, uint256 b) internal pure returns (uint256) {
    return a + b;
}

instead of an earlier implementation as:

 function add(uint a, uint b) internal pure returns (uint) {
    uint c = a + b;
    require(c >= a, "SafeMath: addition overflow");

    return c;
}

or the ones you wrote.

To answer your question: the code you wrote is ok if you are still using a v0.5.0 Solidity compiler, That's because v0.8.0 breaking changes introduced the default check for overflow

Arithmetic operations revert on underflow and overflow. You can use unchecked { ... } to use the previous wrapping behaviour.

Checks for overflow are very common, so we made them the default to increase readability of code, even if it comes at a slight increase of gas costs.

Don't use the updated safemath library from OpenZeppelin with a v0.5.0 compiler.

Also, as a recommendation try to update you compiler version for security, readability and gasCost improvements on your contracts

I wrote another answer to a similar question that may be useful too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.