Results from incorrect conditions in if statements, loops, or switches, causing unintended branching in code execution
e.g. a function meant to handle reward calculation might incorrectly check for block.number >= lastRewardBlock instead of block.number > lastRewardBlock, causing rewards to be skipped or duplicated
Game
Can you identify how this contract might cause certain balances to yield unexpected rewards?
// SPDX-License-Identifier: MIT// Open me in VSCode and really think before opening the hints!// Add @audit tags wherever suspicious// Go to the solidity docs to complete missing knowledge of what's happening here// Solve by drafting a fix!pragmasolidity ^0.8.0;contract IncorrectConditionalsGame {uint256public reward;// Function to calculate rewards based on balancefunctioncalculateReward(uint256 balance) public {if (balance >100) { reward =10; } elseif (balance >500) { reward =50; } else { reward =0; } }}
Consider the order in which each condition is evaluated. Are any conditions unreachable or incorrectly prioritized?
Pay attention to the range of balances and whether each condition logically captures the correct range for each reward tier.
functioncalculateReward(uint256 balance) public {if (balance >500) { // Fix: Check for higher balance first reward =50; } elseif (balance >100) { reward =10; } else { reward =0; }}