Skip to main content

Missing Zero Address Checks

Description

In some smart contracts, developers do not implement checks to prevent the input of zero values in some functions. For example, in the setAdmin() function, a zero value can be passed as the new admin address. If this address is set to zero, the contract may not behave as expected, or worse, could be vulnerable to attacks. It is essential to add zero checks in the smart contract to prevent such unexpected behavior.

Example Code

Consider the following code snippet:

function setAdmin(address newAdmin) public onlyOwner {
require(newAdmin != address(0), "Invalid address");
admin = newAdmin;
}

In this example, the setAdmin() function sets the admin address to the value passed in the newAdmin parameter. The function includes a check to ensure that the input address is not zero.

However, the following implementation does not have a zero check:

function setAdmin(address newAdmin) public onlyOwner {
admin = newAdmin;
}

This implementation allows the admin address to be set to zero, which could lead to unexpected behavior.

Recommendation

It is highly recommended to implement zero checks in smart contracts. Developers should include these checks to prevent the input of zero values in critical functions. In the above example, a zero check should be added to the setAdmin() function to prevent the admin address from being set to zero.

Adding zero checks in smart contracts increases their security and reduces the risk of unexpected behavior.