Eggstravaganza

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

Incorrect time remaining calculation when game ends early

Summary

The getTimeRemaining function calculates the time left based on the scheduled endTime. However, if the game is manually ended early via the endGame function, getTimeRemaining will still report a non-zero remaining time until the original endTime is reached, which is inaccurate.

Vulnerability Details

The getTimeRemaining function calculates the remaining time as the difference between the endTime state variable and the current block.timestamp:

function getTimeRemaining() external view returns (uint256) {
return block.timestamp >= endTime ? 0 : endTime - block.timestamp;
}

The endGame function allows the owner to stop the game prematurely by setting the gameActive flag to false:

function endGame() external onlyOwner {
require(gameActive, "Game not active");
gameActive = false;
emit GameEnded(block.timestamp);
}

Critically, endGame does not update the endTime variable. Consequently, if endGame is called before the initially scheduled endTime, the getTimeRemaining function will continue to return a positive value, suggesting the game is still running for the remainder of the originally scheduled duration, even though gameActive is false.

Impact

This discrepancy can mislead users or front-end applications relying on getTimeRemaining to display the game's status. It might show time remaining for a game that is actually inactive, causing confusion or potentially impacting user interactions based on perceived game state.

Tools Used

Manual Review

Recommendations

Modify the getTimeRemaining function to consider the gameActive status. If the game is not active, it should return 0 regardless of the endTime.

function getTimeRemaining() external view returns (uint256) {
if (!gameActive) {
return 0;
}
return block.timestamp >= endTime ? 0 : endTime - block.timestamp;
}
Updates

Lead Judging Commences

m3dython Lead Judge 4 months ago
Submission Judgement Published
Validated
Assigned finding tags:

Incomplete end game handling

Incorrect values reported when a game is ended early

Support

FAQs

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