Back to Blog

Do I still need SafeMath in Solidity 0.8+?

Financial Toolset Team4 min read

No. Solidity 0.8.0 and later have built-in overflow/underflow protection by default. Arithmetic operations automatically revert on overflow. SafeMath is only needed for Solidity 0.7.x and earlier. ...

Do I still need SafeMath in Solidity 0.8+?

Listen to this article

Browser text-to-speech

Do You Still Need SafeMath in Solidity 0.8+?

With the advent of Solidity 0.8.0, developers working with Ethereum smart contracts received a significant update: built-in overflow and underflow protection. This change raised questions about the continued relevance of the once-essential SafeMath library. In this article, we’ll explore why SafeMath is generally unnecessary for Solidity 0.8+ and what exceptions might still warrant its use.

The Built-in Protection of Solidity 0.8+

Solidity 0.8.0, released in early 2021, introduced automatic checks for overflow and underflow during arithmetic operations. This means that operations like addition, subtraction, and multiplication will automatically revert if they exceed the limits of the integer type being used. This default protection simplifies code, reduces the room for critical vulnerabilities, and eliminates the need for external libraries like SafeMath.

Key Features of Built-in Checks

  • Automatic Reversion: When an arithmetic operation overflows or underflows, the transaction reverts automatically. For example, adding two unsigned integers (e.g., uint256 a = 2**256 - 1; uint256 b = 1;) will revert rather than wrapping around.
  • Cleaner Code: Developers can now write a + b or a - b without wrapping these operations in SafeMath functions, resulting in cleaner and more readable code.
  • Enhanced Safety: The built-in checks make it harder to introduce vulnerabilities due to arithmetic errors, which were historically a significant source of bugs in smart contracts.

Using Unchecked Blocks for Optimization

While Solidity 0.8+ provides safety by default, there are scenarios where developers might want to bypass these checks for specific reasons, such as gas optimization. Solidity allows the use of unchecked blocks to perform unchecked arithmetic deliberately.

When to Use Unchecked Blocks

  • Gas Optimization: In high-frequency operations where you are certain overflow cannot occur, using unchecked can save gas. For example:

    uint256 a = 2**256 - 1;
    unchecked {
        uint256 b = a + 1; // This won't revert
    }
    
  • Legacy Code Compatibility: If migrating a complex contract from a pre-0.8 version, temporary use of unchecked blocks might ease the transition before refactoring for full safety.

Real-World Examples

Since Solidity 0.8.0's release, the majority of new ERC20 token contracts and DeFi projects have omitted SafeMath entirely, leveraging the built-in safety features. For instance, developers of a new DeFi lending platform can confidently perform arithmetic operations for interest calculation without SafeMath, ensuring the operations are secure by default.

ERC20 Token Example

Consider a simple ERC20 contract:

function transfer(address to, uint256 amount) public returns (bool) {
    balances[msg.sender] -= amount;
    balances[to] += amount;
    return true;
}

In Solidity 0.8+, the subtraction and addition in the transfer function automatically include overflow/underflow checks, ensuring safe execution without explicit SafeMath usage.

Important Considerations

Despite the advantages of built-in checks, some considerations remain:

  • Custom Error Messages: Solidity 0.8+ does not support custom revert messages for arithmetic errors. Developers requiring specific error handling might still use SafeMath or custom wrappers.
  • Legacy Contracts: Some legacy systems still depend on SafeMath for compatibility reasons. Gradual migration to native Solidity features can help maintain contract efficiency and safety.
  • Unchecked Arithmetic: While unchecked blocks offer gas savings, they should be used cautiously. Ensure that the logic within such blocks cannot result in unintended overflows.

Bottom Line

For most developers, SafeMath is no longer necessary in Solidity 0.8+ due to the built-in overflow and underflow checks, which promote safer and cleaner code. However, if you require custom error handling or need to optimize for gas costs, consider carefully using SafeMath or unchecked blocks. As the Ethereum community moves forward, embracing Solidity 0.8+ features will likely be the best practice for ensuring smart contract security and efficiency. Always validate that your entire codebase is compatible with Solidity 0.8+ and adjust accordingly.

Try the Calculator

Ready to take control of your finances?

Calculate your personalized results.

Launch Calculator

Frequently Asked Questions

Common questions about the Do I still need SafeMath in Solidity 0.8+?

No. Solidity 0.8.0 and later have built-in overflow/underflow protection by default. Arithmetic operations automatically revert on overflow. SafeMath is only needed for Solidity 0.7.x and earlier. ...