DeFiFoundry
20,000 USDC
View results
Submission Details
Severity: low
Invalid

Unsafe Type Casting in FjordStaking::getEpoch Function

Summary

In the getEpoch function of the FjordStaking contract, there is a potential issue due to unsafe type casting from uint256 to uint16. This casting could lead to unintended behavior, particularly when dealing with large numbers, which could result in overflow errors. If not addressed, this vulnerability might cause incorrect epoch calculations and could impact the correct functioning of the staking and reward distribution mechanisms.

Vulnerability Details

function getEpoch(uint256 _timestamp) public view returns (uint16) {
if (_timestamp < startTime) return 0;
return uint16((_timestamp - startTime) / epochDuration) + 1;
}

The function casts the result of the calculation ( _timestamp - startTime ) / epochDuration from uint256 to uint16 without any checks to ensure the value fits within the uint16 range. This could lead to an overflow if the calculated epoch number exceeds 65535 (the maximum value for a uint16).

Impact

If the calculated epoch value exceeds the maximum limit for a uint16, the type casting will truncate the value, leading to incorrect epoch numbers. This could result in incorrect reward distributions, as the contract might reference the wrong epoch. Potential loss of user funds due to miscalculated reward claims or staking periods.

Tools Used

Manual Review

Recommendations

Use Safe Casting: Implement OpenZeppelin’s SafeCast library to safely cast the result from uint256 to uint16. This will prevent unexpected overflows by reverting the transaction if the value exceeds the uint16 range.

function getEpoch(uint256 _timestamp) public view returns (uint16) {
if (_timestamp < startTime) return 0;
return SafeCast.toUint16((_timestamp - startTime) / epochDuration) + 1;
}
Updates

Lead Judging Commences

inallhonesty Lead Judge
about 1 year ago
inallhonesty Lead Judge about 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.