Skip to content

Division by zero is checked in Unchecked blocks #15200

@Lohann

Description

@Lohann

Description

The division reverts even inside an unchecked block, which is contradictory, what if I already validated that before the unchecked block? or if I rely on the result zero when divide by zero?

Environment

  • Compiler version: 0.8.25
  • Target EVM version (as per compiler settings): 'shanghai'
  • Framework/IDE (e.g. Truffle or Remix): any
  • EVM execution environment / backend / blockchain client: Forge
  • Operating system: MacOS

Steps to Reproduce

The following code reverts when b is zero, which should not happen inside an unchecked block.

pragma solidity >=0.8.0;

contract Example {
    function divide(uint256 a, uint256 b) external pure returns (uint256) {
        unchecked {
            // Reverts when b = 0
            return a / b;
        }
    }
    
    function asmDivide(uint256 a, uint256 b) external pure returns (uint256 r) {
        assembly {
            // Works when b = 0
            r := div(a, b)
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions