solang icon indicating copy to clipboard operation
solang copied to clipboard

Pointer to uninitialized External Function struct should not be null

Open LucasSte opened this issue 3 years ago • 3 comments

This contract causes an access violation in Solana's mock VM, because fun is a null pointer.

contract C {

    function test(uint256 newAddress, bytes4 newSelector) public view returns (bytes4, address) {
        function() external fun;
        address myAddr = address(newAddress);
        assembly {
            fun.selector := myAddr
            fun.address  := newAddress
        }

        return (fun.selector, fun.address);
    }
}

LucasSte avatar Aug 01 '22 12:08 LucasSte

This won't even compile with solc:

Error: Explicit type conversion not allowed from "uint256" to "address".
 --> bug.sol:7:26:
  |
7 |         address myAddr = address(newAddress);
  |                          ^^^^^^^^^^^^^^^^^^^

xermicus avatar Jun 29 '23 10:06 xermicus

This won't even compile with solc:

Error: Explicit type conversion not allowed from "uint256" to "address".
 --> bug.sol:7:26:
  |
7 |         address myAddr = address(newAddress);
  |                          ^^^^^^^^^^^^^^^^^^^

That's because an address is 20 bytes on ethereum. I think this will work:

contract C {

    function test(uint160 newAddress, bytes4 newSelector) public view returns (bytes4, address) {
        function() external fun;
        address myAddr = address(newAddress);
        assembly {
            fun.selector := myAddr
            fun.address  := newAddress
        }

        return (fun.selector, fun.address);
    }
}

seanyoung avatar Jun 29 '23 11:06 seanyoung

Of course, maybe next time I should read the contract first

xermicus avatar Jun 29 '23 13:06 xermicus