0
0
Blockchain / Solidityprogramming~10 mins

Integer overflow and underflow in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to prevent integer overflow by using a safe addition function.

Blockchain / Solidity
function safeAdd(uint8 a, uint8 b) public pure returns (uint8) {
    uint8 c = a + [1];
    require(c >= a, "Overflow detected");
    return c;
}
Drag options to blanks, or click blank then click option'
A1
Ba
Cb
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name in the addition.
Adding a constant instead of the variable.
2fill in blank
medium

Complete the code to check for underflow before subtraction.

Blockchain / Solidity
function safeSub(uint8 a, uint8 b) public pure returns (uint8) {
    require(a [1] b, "Underflow detected");
    return a - b;
}
Drag options to blanks, or click blank then click option'
A>=
B<
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>=' causing incorrect underflow checks.
Using '==' which is too strict.
3fill in blank
hard

Fix the error in the code that causes overflow when multiplying two uint8 numbers.

Blockchain / Solidity
function safeMul(uint8 a, uint8 b) public pure returns (uint8) {
    uint8 c = a * [1];
    require(b == 0 || c / b == a, "Overflow detected");
    return c;
}
Drag options to blanks, or click blank then click option'
A0
Ba
C1
Db
Attempts:
3 left
💡 Hint
Common Mistakes
Multiplying a by itself instead of b.
Using a constant instead of the variable.
4fill in blank
hard

Fill both blanks to create a safe division function that prevents division by zero.

Blockchain / Solidity
function safeDiv(uint8 a, uint8 b) public pure returns (uint8) {
    require(b [1] 0, "Division by zero");
    return a [2] b;
}
Drag options to blanks, or click blank then click option'
A!=
B/
C==
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if b equals zero instead of not equals.
Using multiplication instead of division.
5fill in blank
hard

Fill all three blanks to create a safe modulo function that prevents modulo by zero.

Blockchain / Solidity
function safeMod(uint8 a, uint8 b) public pure returns (uint8) {
    require(b [1] 0, "Modulo by zero");
    uint8 c = a [2] b;
    return c [3] b;
}
Drag options to blanks, or click blank then click option'
A!=
B%
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition or subtraction instead of modulo.
Not checking for zero divisor.