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.
smartcontract Article's
30 articles in total
Favicon
Vyper Data Types (Series 2)
Favicon
Vyper is redefining smart contract development with its focus on simplicity, security, and efficiency. With its rapidly growing community of developers, Vyper is becoming the go-to for smart contract development, creating a thriving ecosystem.
Favicon
Have You Fallen for a Phishing Scam? Letโ€™s Talk About It ๐Ÿ‘€
Favicon
How much does smart contract development cost?
Favicon
OverFlow and UnderFlow causes in Solidity
Favicon
INTEGRATION OF DAPPS WITH MODE: WALLET AND SMART CONTRACTS | PART 2: CONNECTING SMART CONTRACTS TO THE FRONT-END
Favicon
Web3 and Blockchain Development: Unlocking the Power of Decentralized Applications
Favicon
OverFlow and UnderFlow causes in Solidity
Favicon
Need some help for getting Web3 internship
Favicon
The Danger of Randomness in Smart Contracts and its solution
Favicon
What is Reentrancy?
Favicon
Smart Contract Upgrades: Enhance Next-Level Blockchain Security
Favicon
Smart Contract Audits: Ensuring Safe Blockchain Migration
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
A Walkthrough of Solidity Custom Errors
Favicon
How to write dynamic staking smart contract step by step in practice
Favicon
How to migrate smart contracts to a blockchain?
Favicon
Every Blockchain Developer Must Know About This Scam!
Favicon
Smart Contract Migration: Essential Steps for Secure Upgrades
Favicon
Basic understanding of Dynamic Staking
Favicon
INTEGRATION OF DAPPS WITH MODE: WALLET AND SMART CONTRACTS | PART 1: CONNECTING THE WALLET
Favicon
A Step-by-Step Guide to Automating Smart Contract Upgrades with Deep Storage Data
Favicon
Blockchain-based FinTech Solution
Favicon
How Deep Storage Data Transforms Smart Contract Migration
Favicon
How to Move Smart Contracts From Existing Blockchain Platforms to New Ones?
Favicon
How to Write a Token Price Oracle Smart Contract
Favicon
Future of Smart Contract development: Trends and Predictions forย 2025
Favicon
Smart Contract Safety: How to Verify Before You Interact
Favicon
Smart Contract Security Audit using Deep Storage Data

Featured ones: