Integer overflow and underflow in Blockchain / Solidity - Time & Space Complexity
When working with numbers in blockchain, it is important to understand how operations on integers behave as the input size changes.
We want to know how the time to check or handle integer overflow or underflow grows when numbers get bigger.
Analyze the time complexity of the following code snippet.
function safeAdd(uint256 a, uint256 b) public pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "Overflow detected");
return c;
}
This function adds two numbers and checks if the result caused an overflow by comparing it to the first number.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single addition and comparison.
- How many times: Exactly once per function call, no loops or recursion.
The time to add two numbers and check for overflow stays the same no matter how large the numbers are.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 2 (add + compare) |
| 100 | 2 (add + compare) |
| 1000 | 2 (add + compare) |
Pattern observation: The number of operations does not increase with input size; it stays constant.
Time Complexity: O(1)
This means the time to check for overflow or underflow does not grow as the numbers get bigger; it takes the same amount of time every time.
[X] Wrong: "Adding bigger numbers takes more time because the numbers are larger."
[OK] Correct: Computers handle fixed-size numbers in constant time, so addition and comparison take the same time regardless of number size.
Understanding that integer operations run in constant time helps you explain how smart contracts handle numbers safely and efficiently.
"What if we changed from fixed-size integers to arbitrary large integers? How would the time complexity change?"
Practice
unsigned integer variable exceeds its maximum value?Solution
Step 1: Understand unsigned integer limits
Unsigned integers start at 0 and go up to a max value (like 2^256-1 in Solidity).Step 2: What happens when max is exceeded?
When the value goes above max, it wraps around to 0, causing integer overflow.Final Answer:
It wraps around to zero (integer overflow). -> Option AQuick Check:
Unsigned integer overflow = wrap to zero [OK]
- Thinking overflow throws an error automatically
- Assuming value becomes negative
- Believing value stays at max
balance?Solution
Step 1: Identify safe subtraction
To avoid underflow, subtract only if balance is greater than zero.Step 2: Check each option
if (balance > 0) { balance -= 1; } checks balance > 0 before subtracting, preventing underflow. The other options either subtract without checking first (allowing underflow) or add instead of subtracting.Final Answer:
if (balance > 0) { balance -= 1; } -> Option CQuick Check:
Check before subtracting prevents underflow [OK]
- Not checking before subtracting
- Using require after subtraction (too late)
- Adding instead of subtracting
uint8 x = 255; x += 1;
What is the value of
x after execution?Solution
Step 1: Understand uint8 max value
uint8 can hold values from 0 to 255. 255 is the max.Step 2: Add 1 to max value
Adding 1 causes overflow, wrapping value back to 0.Final Answer:
0 -> Option AQuick Check:
uint8 overflow wraps to 0 [OK]
- Expecting 256 (out of range)
- Expecting value to stay at 255
- Confusing with signed integers
count. What is the bug?function decrement(uint count) public pure returns (uint) {
count -= 1;
return count;
}Solution
Step 1: Analyze subtraction without check
The function subtracts 1 from count without checking if count is zero.Step 2: Understand underflow risk
If count is 0, subtracting 1 causes underflow, wrapping to max uint value.Final Answer:
Underflow if count is 0, wrapping to max uint. -> Option BQuick Check:
Subtracting 1 from 0 causes underflow [OK]
- Assuming no bug without checks
- Thinking Solidity throws error automatically
- Confusing underflow with negative return
uint256. You want to safely add tokens without overflow. Which approach is best?Solution
Step 1: Understand overflow risk in addition
Adding tokens can overflow uint256 max value, causing wraparound bugs.Step 2: Use SafeMath library
SafeMath's add function checks for overflow and reverts if it happens, preventing bugs.Step 3: Evaluate other options
Unchecked addition risks overflow. Subtracting first or converting to int256 does not prevent overflow safely.Final Answer:
Use SafeMath library's add function to check overflow. -> Option DQuick Check:
SafeMath add prevents overflow [OK]
- Using unchecked addition risking overflow
- Trying to subtract before adding
- Converting to signed int without checks
Ready to prove it?
Pass the test and this pattern moves unknown
