The Danger of not checking for address zero in smart contract. (2024)

In Solidity, the zero address (also known as the null address) is a special address that represents an uninitialized or burn address. It is represented as 0x0 or 0x0000000000000000000000000000. It has no private key, so any token deposited into the address cannot be recovered.

Why do we need to check for the zero address

  1. To prevent loss of ownership. In the code snippet below we have a function updateOwner which is used to change the owner. if there is no address zero check for the parameters, the ownership of the contract will be lost forever and every other function that requires the owner/admin to call will be reverted because the owner has been changed to address zero.
// SPDX-License-Identifier: UNLICENSEDpragma solidity ^0.8.4;contract Test { address public owner; error notadmin(string); constructor(){ owner = msg.sender; } modifier onlyAdmin { if (msg.sender != owner) { revert notadmin("not admin"); } _; } function updateOwner(address newOwner) onlyAdmin external { // Check for address 0 require(newOwner != address(0), "Invalid address"); owner = newOwner; }}
  1. address zero checks can be useful in situations where you need to ensure that a valid address is passed to a function before making a call to another function or contract.
// SPDX-License-Identifier: UNLICENSEDpragma solidity ^0.8.0;contract Test { function doSomething(address _addr) public { // Check for address 0 require(_addr != address(0), "Invalid address"); // Call another function if the address is valid _addr.call(abi.encodeWithSignature("doSomething(address)", _addr)); } function doSomethingElse() public returns(address){ // Function code goes here return msg.sender; }}
  1. To prevent loss of tokens. sending tokens to address zero will result in a loss of funds. if the _to/receiver address is not checked against address zero when transferring a token and a user mistakenly sends a token to address zero. The token will be lost forever.

Furthermore, in Solidity, the address zero is used as a placeholder for some types of data. For example, the address zero is frequently used as a default value for uninitialized contract storage variables. In these circ*mstances, looking for address zero might help you confirm that your contract is handling uninitialized data correctly.

Nomad Bridge attack which cost the crypto market about $190M of liquidity drained was caused by lack of address zero check read more

Another example of an exploit that involved a missing address 0 check: https://medium.com/@QubitFin/protocol-exploit-report-305c34540fa3 3

How to check for zero address in your contract?

You can use the iszero built-in function in Solidity, which returns a boolean indicating whether an address is the zero address. The iszero function was introduced in Solidity version 0.8.0.

if (iszero(_userAddress)) { // _userAddress is the zero address}// it returns 'true' if _userAddress is the zero address and 'false' otherwise.

It can also be done using the equality operator (==) and the zero address literal:

if (_userAddress == 0x0) { // _userAddress is the zero address}

kindly like and share.

The Danger of not checking for address zero in  smart contract. (2024)
Top Articles
Generic Routing Encapsulation (GRE) Tunnel - Study CCNP
Should You Reaffirm a Mortgage in Bankruptcy?
The Tribes and Castes of the Central Provinces of India, Volume 3
Dte Outage Map Woodhaven
Uti Hvacr
Ghosted Imdb Parents Guide
Celebrity Extra
How to know if a financial advisor is good?
Barstool Sports Gif
Housing Intranet Unt
Top Hat Trailer Wiring Diagram
Everything You Need to Know About Holly by Stephen King
Summoner Class Calamity Guide
Immortal Ink Waxahachie
Cambridge Assessor Database
Spider-Man: Across The Spider-Verse Showtimes Near Marcus Bay Park Cinema
Alfie Liebel
Between Friends Comic Strip Today
Cincinnati Adult Search
[PDF] NAVY RESERVE PERSONNEL MANUAL - Free Download PDF
Holiday Gift Bearer In Egypt
How Long After Dayquil Can I Take Benadryl
Del Amo Fashion Center Map
Low Tide In Twilight Ch 52
Craigslist Apartments In Philly
Dmv In Anoka
Star Wars Armada Wikia
Safeway Aciu
Stockton (California) – Travel guide at Wikivoyage
His Only Son Showtimes Near Marquee Cinemas - Wakefield 12
Osrs Important Letter
Salemhex ticket show3
Mia Malkova Bio, Net Worth, Age & More - Magzica
Fairwinds Shred Fest 2023
Www Violationinfo Com Login New Orleans
Jefferson Parish Dump Wall Blvd
Ticketmaster Lion King Chicago
Engr 2300 Osu
How to Quickly Detect GI Stasis in Rabbits (and what to do about it) | The Bunny Lady
Catchvideo Chrome Extension
What is 'Breaking Bad' star Aaron Paul's Net Worth?
The Pretty Kitty Tanglewood
Page 5747 – Christianity Today
Greg Steube Height
Diamond Spikes Worth Aj
Besoldungstabellen | Niedersächsisches Landesamt für Bezüge und Versorgung (NLBV)
Compete My Workforce
March 2023 Wincalendar
8663831604
Escape From Tarkov Supply Plans Therapist Quest Guide
Códigos SWIFT/BIC para bancos de USA
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 6704

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.