There is an unsafe cast in PuppyRaffle::selectWinner. The uint256 fee variable is casted to uint64. If the value of fee variable is greater than the max value that can be stored in uint64, the uint64 fee variable will become zero and totalFees will be incorrect calculated.
In selectWinner() function there is a an unsafe cast of fee variable from uint256 to uint64. If the uint256 value is larger than the maximum value a uint64 can hold, the casting operation will result in an overflow error. This could lead to incorrect calculations of the totalFees and lost fees.
If the fee exceeds the maximum value a uint64 can hold, the calculation of the totalFees will be incorrect. This could lead to the contract distributing less fees than it should, which could result in financial losses for the contract owner and the players.
Also, if the overflow issue is exploited by a malicious actor, he could potentially manipulate the balances of the contract and the players. For example, a malicious actor could enter the raffle with a large number of players, causing the fee to overflow and the contract to distribute less fees than it should. This could result in financial losses for the contract owner and the players.
The following test demonstrates the unsafe cast from uint256 to uint64 in the selectWinner() function. It shows that a uint256 value that exceeds the maximum value a uint64 can hold can be cast to a uint64 without any errors, but the result is an overflow, where the uint64 value is set to 0. Add the test function testUnsafeCastOverflowInSelectWinner() to the file PuppyRaffleTest.t.sol.
VS Code, Foundry
To mitigate these risks, you can use the OpenZeppelin SafeCast library to safely cast between different types. This library provides functions that check for overflow and underflow conditions before performing the cast, preventing data loss and overflow errors.
Here's an example of how you could use the SafeCast library to safely cast a uint256 to a uint64:
This function will revert if the uint256 value is larger than the maximum value a uint64 can hold, preventing data loss and overflow errors.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.