5

Take the following code snippet:

pragma solidity ^0.8.0;

contract MyContract {
    function foo() public returns (uint256 result) {
        // do unsafe work
    }

    function bar() public returns (uint256 result) {
        unchecked {
            uint256 foo_value = foo();
            // do safe work
        }
    }

Does the unchecked block in the bar function disable the safety checks in the foo function?

2 Answers 2

4

Looking at the documentation for Solidity 0.8.0 it doesn't seem so like the unchecked block in bar disables the safety checks in foo..

The setting only affects the statements that are syntactically inside the block. Functions called from within an unchecked block do not inherit the property.

https://docs.soliditylang.org/en/v0.8.0/control-structures.html#checked-or-unchecked-arithmetic

1

It looks like it only applies to current block:

pragma solidity v0.8.9;

contract Test {
function unsafe_subtract(uint a, uint b) pure public returns (uint) {
    unchecked {
      return subtract(a,b);
    }
  }

  function subtract(uint a, uint b) pure public returns (uint) {
    return a - b;
  }
}

Calling unsafe_subtract(1, 100) reverted.

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.