Beginner FriendlyFoundryNFT
100 EXP
View results
Submission Details
Severity: low
Invalid

Call to `HorseStore.sol::isHappyHorse()` at block timestamp less than `86400` will revert due to the arithmetic underflow

Summary

Calculation logic to determine horse happiness will revert the transaction if executed before block timestamp 86400.

Vulnerability Details

There is an issue in the HorseStore.sol::isHappyHorse() function implementation where the calculation won't work when executed at a block timestamp earlier than 86400. This is due to an arithmetic underflow problem, which causes the function to revert.

Impact

It wouldn't be possible to obtain a result from the HorseStore.sol::isHappyHorse() function.

Proof of Concept (PoC)

Add the next test in HorseStoreSolidity.t.sol.

import {stdError} from "forge-std/StdError.sol";
function test_IsHappyHorseRevertsWhenExecutedAtBlockTimestampLessThan86400(uint256 horseId, uint256 checkAt) public {
checkAt = bound(checkAt, 0, horseStore.HORSE_HAPPY_IF_FED_WITHIN() - 1 seconds); // timestamp less than 86400
vm.warp(checkAt);
vm.expectRevert(stdError.arithmeticError);
horseStore.isHappyHorse(horseId);
}

Run a test with forge test --mt test_IsHappyHorseRevertsWhenExecutedAtBlockTimestampLessThan86400.

Tools Used

  • Foundry

Recommendations

Recommended changes in HorseStore.sol::isHappyHorse() function:

function isHappyHorse(uint256 horseId) external view returns (bool) {
- (horseIdToFedTimeStamp[horseId] <= block.timestamp - HORSE_HAPPY_IF_FED_WITHIN) {
- return false;
- }
- return true;
+ return HORSE_HAPPY_IF_FED_WITHIN > block.timestamp - horseIdToFedTimeStamp[horseId];
}

Add the next test in HorseStoreSolidity.t.sol.

function test_IsHappyHorseExecutedAtBlockTimestampLessThan86400(uint256 horseId, uint256 checkAt) public {
checkAt = bound(checkAt, 0, horseStore.HORSE_HAPPY_IF_FED_WITHIN() - 1 seconds); // timestamp less than 86400
vm.warp(checkAt);
horseStore.isHappyHorse(horseId);
}

Run a test with forge test --mt test_IsHappyHorseExecutedAtBlockTimestampLessThan86400.

Updates

Lead Judging Commences

inallhonesty Lead Judge over 1 year ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

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