2

I have this fragment of code from Ethernaut (link):

pragma solidity ^0.4.18;
...
 function() public {
    if(delegate.delegatecall(msg.data)) {
      this;
    }
  }

What's means this; ?

  • Similar question here. Still with no answer – Masoud jt Jun 27 at 3:11
  • 1
    I think it has no meaning, but I might be wrong. I have seen people use it to avoid the no-empty-blocks solium warning: ethlint.readthedocs.io/en/latest/…. – Markus - soliditydeveloper.com Jun 27 at 3:31
  • @Markus-soliditydeveloper.com If the block is necessary, I can understand that reason, but it doesn't seem like the if is needed for this example. I believe it's just meant to demonstrate how a delegatecall to another contract is a security risk. – Chan-Ho Suh Jun 27 at 3:55
  • 1
    It's usually in order to prevent compiler warning about a view function which can be restricted to pure. – goodvibration Jun 27 at 7:01
3

It is typically in order to workaround compiler warning about a view function which can be restricted to pure, for example:

uint256[4] public arr;

constructor() public {
    arr[0] = 11;
    arr[1] = 22;
    arr[2] = 33;
    arr[3] = 44;
}

function func(uint256 x) public view returns (uint256) {
    if (x < arr.length)
        return handle(x, viewFunc);
    return handle(x, pureFunc);
}

function handle(uint256 x, function (uint256) view returns (uint256) func) internal view returns (uint256) {
    return func(x);
}

function viewFunc(uint256 x) internal view returns (uint256) {
    return arr[x];
}

function pureFunc(uint256 x) internal view returns (uint256) {
    this;
    return x * 2;
}

As you can see, function handle takes two input arguments:

  1. uint256 x
  2. function (uint256) view returns (uint256) func, which is a pointer to a view function

Now, suppose you wanted to call function handle and pass it a pure function. You could do this by declaring that pure function as view, but the compiler would issue a warning on that. So in order to revoke that warning, you simply add this; in the implementation of the pure function.

In your specific example, however, since the function is not even view, I am not so sure about the reason for adding this; in the implementation of this function. It obviously has no runtime impact.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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