Eggstravaganza

First Flight #37
Beginner FriendlySolidity
100 EXP
View results
Submission Details
Severity: low
Invalid

Informational::Unreachable Code in Game Status Function

Summary

The getGameStatus() function in the EggHuntGame contract contains a conditional branch that can never be executed, leading to dead code in the contract.

Vulnerability Details

Specifically, the condition block.timestamp < startTime within the gameActive == true block is logically unreachable:

function getGameStatus() external view returns (string memory) {
if (gameActive) {
if (block.timestamp < startTime) {
return "Game not started yet";
} else if (block.timestamp >= startTime && block.timestamp <= endTime) {
return "Game is active";
} else {
return "Game time elapsed";
}
} else {
return "Game is not active";
}
}

This condition is unreachable because in the startGame() function, gameActive is set to true at the same time as startTime is set to the current block.timestamp:

function startGame(uint256 duration) external onlyOwner {
require(!gameActive, "Game already active");
require(duration >= MIN_GAME_DURATION, "Duration too short");
startTime = block.timestamp;
endTime = block.timestamp + duration;
gameActive = true;
emit GameStarted(startTime, endTime);
}

Impact

  • Dead code increases contract size unnecessarily

  • Potential confusion for developers maintaining the code

  • Slight increase in deployment gas costs

By analyzing the contract logic, we can see that:

  1. gameActive is initially false

  2. gameActive is set to true only in the startGame() function

  3. In that same function, startTime is set to the current block.timestamp

  4. Therefore, when gameActive is true, block.timestamp cannot be less than startTime

Tools Used

Manual review

Recommendations

Remove the unreachable condition and simplify the function:

function getGameStatus() external view returns (string memory) {
if (gameActive) {
if (block.timestamp <= endTime) {
return "Game is active";
} else {
return "Game time elapsed";
}
} else {
return "Game is not active";
}
}

This change makes the code more concise and easier to understand while maintaining the same functionality.

Updates

Lead Judging Commences

m3dython Lead Judge 2 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.