1

I have a contract (Implementation.sol) which delegatecalls the other contract (Attacker.sol) to selfdestruct. When I try to compile the Implementation.sol on Remix, I receive Expected primary expression error for the line require(success && dat != byte(0x0), "call failed");. Not sure what to do.

contract Implementation {

  function delegateTheCall (address target, string calldata action, address to) public returns (bytes memory) {

    (bool success,bytes memory dat) = target.delegatecall(abi.encodeWithSignature(action, to));
    require(success && dat != byte(0x0), "call failed"); 
    return dat;
  }
2
  • You probably want dat.length != 0 instead of dat != byte(0x0). But why having this condition? It's common for a function to return nothing and still succeed.
    – 0xSanson
    Commented Aug 27, 2022 at 14:36
  • 1
    @0xSanson This is a better way to check the value. You're right. Thank you a lot. I'm just trying some experimental contracts to understand the concepts better. I definitely will not use these in prod.
    – bic
    Commented Aug 27, 2022 at 16:30

1 Answer 1

0

It's tricky to compare strings and bytes in Solidity. These two types are treated exactly the same in Solidity, only that string gets decoded in UTF-8 when returning it outside of the smart contract.

To compare these types, use keccak256:

require(success && keccak256(dat) != keccak256(""), "call failed"); 

The empty string ("") is treated as an empty bytes array, which is what you need.

Try it doing something like this to check the result and you'll see what I mean. Comparing the keccak256 hash of an an empty bytes array with the keccak256 hash of an empty string (which is the same as an empty bytes array):

bytes memory dat;
require(keccak256(dat) != keccak256(""));
1
  • 1
    Thank you @Jeremy Then. I definitely missed the point that you reminded me. It worked.
    – bic
    Commented Aug 27, 2022 at 16:27

Your Answer

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

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