1

The function selector are the first four bytes of the keccak256 hash of the function signature. What is the signature of the fallback function? It neither has a name nor any inputs.
If there is no real signature there have to be at least a custom function selector (some fixed 4 bytes reserved for the fallback function).

2 Answers 2

4

There is no selector. The fallback function is called if the contract does not recognise the selector, or if no selector is provided.

1

If you need to pass data to the fallback, you should fill in a dummy selector value. Lots of answers to this question incompletely state that no selector needs be provided. However, the first four bytes of message calldata is defined by the callee contract as function signature. Additionally, fallback implementations usually assume (as does the compiler implementation to some extent, i.e. implying with msg.sig) that the first 4 bytes are to be discarded, before processing bytes data.

Consider what happens if you encode and send data without a selector: the callee contract uses the first four bytes of calldata msg.data)-let's say 0x12345678...-as the function selector to try and match to any existing named functions. If the contract fails find a match, then it will default to the fallback if it exists. At this point, most fallback logic will remove the first 4 bytes and decode the rest of the data as values (abi.decode(bytes[4:],...<value types>)). If the fallback isn't authored by yourself, check the implementation to see if it does strip the first 4 bytes.

Why should a dummy selector always be included? Because there's a small chance that data sent without a selector may match one of the named functions in the contract and at best fail, and at worst, lose someone a ton of money with unintended alterations of storage. The chances of this are about one in a billion, so it only matters when working on a big project expected to handle millions, if not billions of dollars. However, this is good coding practice none the less. Find a fixed dummy selector value-anything will do, e.g.0x00000000-and prepend that to all data that's been encoded for the fallback call and you'll have reliable function selection every time.

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.