Logo

dev-resources.site

for different kinds of informations.

OverFlow and UnderFlow causes in Solidity

Published at
1/6/2025
Categories
solidity
ethereum
smartcontract
security
Author
mahima_thacker
Author
14 person written this
mahima_thacker
open
OverFlow and UnderFlow causes in Solidity

In Solidity, arithmetic overflow and underflow were common issues before version 0.8.0.

Here's a quick overview of UnderFlow and OverFlow:

What is Overflow and Underflow?

Overflow:

When you add 1 to the maximum value of a uint8 (255), it exceeds the range (0-255). Solidity doesn't store values beyond 255, so it wraps back to 0.

Have a look at a function called overflow from the attached image, and the result you will get is 0

Image description

Underflow:

When you subtract 1 from the minimum value of a uint8 (0), it goes below the range (negative values aren't allowed). Solidity wraps back to 255.

so this is what we get when we call the function underflow from an image

Image description

But in Solidity 0.8.0 or Above it reverts with an error (overflow/underflow checks are enabled).

Why Does This Matter?

Overflow and underflow could lead to severe vulnerabilities in smart contracts, especially in financial calculations.

How to Prevent This? ๐Ÿ•ตโ€โ™€๏ธ

  • Use Solidity 0.8.0 or above, where overflow/underflow checks are built-in.
  • For older versions, use libraries like OpenZeppelinโ€™s SafeMath.
  • Test edge cases to ensure safety in arithmetic operations.
solidity Article's
30 articles in total
Favicon
Have You Fallen for a Phishing Scam? Letโ€™s Talk About It ๐Ÿ‘€
Favicon
Solidity
Favicon
Why Every Crypto Developer Should Understand Tokenomics ๐Ÿš€
Favicon
How we used the ERC-2535 Diamonds at Proof of Peacemaking Protocol
Favicon
๐Ÿ” Solidity Limitations, Solutions, Best Practices and Gas Optimization ๐Ÿš€
Favicon
go่ฐƒ็”จsolidityๅˆ็บฆๆ–ฐๆ–นๆณ•
Favicon
โš–๏ธ The Importance of Using ReentrancyGuard in Solidity Smart Contract
Favicon
Formal Verification: An Example
Favicon
OverFlow and UnderFlow causes in Solidity
Favicon
When to Use ERC-721 vs ERC-1155: Choosing the Right NFT Standard
Favicon
Solidity Pattern - Proxy Delegate and Decorator Patterns
Favicon
Solidity Patterns - CEI
Favicon
Foundry vs Hardhat (A story version)
Favicon
Energy NFT Marketplace
Favicon
OverFlow and UnderFlow causes in Solidity
Favicon
๐Ÿš€ Getting Started with kritisi CLI: An AI-Driven Security Tool for Solidity
Favicon
Formal Verification: The Foundation of Ethereum Smart Contracts
Favicon
The Danger of Randomness in Smart Contracts and its solution
Favicon
What is Reentrancy?
Favicon
Understanding approve and depositCollateral: The Core of ERC-20 Token Transfers in Solidity
Favicon
Ethereum Transaction Calls and State Changes
Favicon
Creating a Toy Solidity compiler and running it in a Toy EVM
Favicon
Send Tokens in Bulk with Low Fees and Fast Delivery: The Ultimate Airdrop Tool for 2024
Favicon
๐Ÿ›ก๏ธ Why Using OpenZeppelin in Smart Contracts Is Essential
Favicon
The delegatecall Function in Solidity
Favicon
The delegatecall Function in Solidity
Favicon
A Walkthrough of Solidity Custom Errors
Favicon
How to write dynamic staking smart contract step by step in practice
Favicon
Mainnet Forking in Foundry
Favicon
Every Blockchain Developer Must Know About This Scam!

Featured ones: