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?
Ethereum Stack Exchange is a question and answer site for users of Ethereum, the decentralized application platform and smart contract enabled blockchain. It only takes a minute to sign up.
Sign up to join this communitySince 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?
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
: