1

Since Solidity v0.8, the compiler has checks for {over,under}flow by default for all arithmetic operations.

Do these checks apply to assembly (yul) arithmetic operations too?

1 Answer 1

3

No, they apply only to non-assembly code. Check for yourself on Remix:

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;

contract Mul {
    function mul_normal(uint8 x, uint8 y) external pure returns (uint8 result) {
        result = x * y;    
    }
    
    function mul_assembly(uint8 x, uint8 y) external pure returns (uint8 result) {
        assembly {
            result := mul(x, y)
        }
    }
}

Passing 255 and 2 as x and y, respectively, reverts when calling mul_normal but yields 254 when calling mul_assembly:

Remix Screenshot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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