0
0
Blockchain / Solidityprogramming~10 mins

Integer overflow and underflow in Blockchain / Solidity - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Integer overflow and underflow
Start with integer value
Perform arithmetic operation
Check if result > max value?
YesOverflow occurs: wraps to min value
Result is min value
Check if result < min value?
YesUnderflow occurs: wraps to max value
Result is max value
Result is within range
Use result
End
Start with an integer, do math, then check if it goes beyond max or min limits. If yes, overflow or underflow happens, wrapping the value around.
Execution Sample
Blockchain / Solidity
uint8 x = 255;
x = x + 1;
// x wraps to 0 due to overflow
Add 1 to the max value of an 8-bit unsigned integer, causing overflow and wrapping to 0.
Execution Table
StepVariable xOperationResultOverflow/Underflow
1255Initial value255No
2255 + 1Add 10Overflow: wraps to 0
30Final value0No
💡 After adding 1 to 255, overflow occurs and value wraps to 0, which is within range.
Variable Tracker
VariableStartAfter Step 2Final
x25500
Key Moments - 2 Insights
Why does adding 1 to 255 result in 0 instead of 256?
Because the variable is an 8-bit unsigned integer with max 255, adding 1 causes overflow and wraps the value back to 0, as shown in execution_table step 2.
What happens if we subtract 1 from 0 in an unsigned 8-bit integer?
It causes underflow, wrapping the value to 255, the max value. This is similar to overflow but in the opposite direction.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of x after adding 1 to 255?
A0
B256
C255
D1
💡 Hint
Check execution_table row 2 under 'Result' column.
At which step does overflow occur according to the execution table?
AStep 1
BStep 2
CStep 3
DNo overflow occurs
💡 Hint
Look at the 'Overflow/Underflow' column in execution_table.
If x was a signed 8-bit integer with max 127, what would happen if x = 127 and we add 1?
AValue becomes 128 without issue
BOverflow occurs and value wraps to -128
CUnderflow occurs and value wraps to 127
DProgram throws an error
💡 Hint
Signed 8-bit integers overflow wraps from max positive to min negative, similar to unsigned wrapping shown in variable_tracker.
Concept Snapshot
Integer overflow and underflow happen when a number goes beyond its storage limits.
Unsigned integers wrap from max to min on overflow, and min to max on underflow.
Example: uint8 max is 255; adding 1 wraps to 0.
Signed integers wrap between positive max and negative min.
Always check for overflow/underflow in blockchain to avoid bugs.
Full Transcript
Integer overflow and underflow occur when a number exceeds its allowed range. For example, an 8-bit unsigned integer can hold values from 0 to 255. Adding 1 to 255 causes overflow, wrapping the value back to 0. Similarly, subtracting 1 from 0 causes underflow, wrapping to 255. This wrapping behavior is important in blockchain programming because it can cause unexpected bugs or vulnerabilities. The execution table shows step-by-step how the value changes and when overflow happens. Understanding this helps prevent errors in smart contracts and other blockchain code.