The PuppyRaffle::totalFees variable is declared as uint64, which can only hold values up to ~18.4 ETH. In the selectWinner function, fees are calculated as a uint256 but then unsafely cast to uint64. When a single raffle generates more than 18.4 ETH in fees (20% of pot), the cast causes an integer overflow, silently wrapping the value and losing the majority of fee revenue.
Expected behavior: All protocol fees should be accurately tracked in totalFees regardless of raffle size.
Actual behavior: When fees exceed uint64 max (~18.4 ETH), the value wraps around to a small number. For example, 20 ETH in fees gets stored as only 1.55 ETH, losing 18.45 ETH. This occurs in Solidity 0.7.6 which lacks automatic overflow protection.
With 100 players at 1 ETH entrance fee:
Total pot: 100 ETH
Fee (20%): 20 ETH
uint64(20 ETH) overflows to 1.55 ETH
Lost revenue: 18.45 ETH per raffle
Likelihood:
Medium-High - Requires only 92+ players to trigger (92 * 1 ETH * 20% = 18.4 ETH)
Popular raffles will easily exceed this threshold
Silent failure - no revert, just lost funds
Impact:
Direct loss of protocol fee revenue
With 100 players: lose 18+ ETH per raffle
Owner cannot recover lost fees
Accumulates over multiple raffles
Add to test/PuppyRaffleTest.t.sol:
Run: forge test --match-test test_totalFeesOverflow -vv --offline
Output:
The protocol expected to collect 20 ETH in fees but only stored 1.55 ETH due to the overflow, losing 18.45 ETH.
Change totalFees from uint64 to uint256:
This eliminates the unsafe cast and allows totalFees to handle arbitrarily large values without overflow.
## Description ## Vulnerability Details The type conversion from uint256 to uint64 in the expression 'totalFees = totalFees + uint64(fee)' may potentially cause overflow problems if the 'fee' exceeds the maximum value that a uint64 can accommodate (2^64 - 1). ```javascript totalFees = totalFees + uint64(fee); ``` ## POC <details> <summary>Code</summary> ```javascript function testOverflow() public { uint256 initialBalance = address(puppyRaffle).balance; // This value is greater than the maximum value a uint64 can hold uint256 fee = 2**64; // Send ether to the contract (bool success, ) = address(puppyRaffle).call{value: fee}(""); assertTrue(success); uint256 finalBalance = address(puppyRaffle).balance; // Check if the contract's balance increased by the expected amount assertEq(finalBalance, initialBalance + fee); } ``` </details> In this test, assertTrue(success) checks if the ether was successfully sent to the contract, and assertEq(finalBalance, initialBalance + fee) checks if the contract's balance increased by the expected amount. If the balance didn't increase as expected, it could indicate an overflow. ## Impact This could consequently lead to inaccuracies in the computation of 'totalFees'. ## Recommendations To resolve this issue, you should change the data type of `totalFees` from `uint64` to `uint256`. This will prevent any potential overflow issues, as `uint256` can accommodate much larger numbers than `uint64`. Here's how you can do it: Change the declaration of `totalFees` from: ```javascript uint64 public totalFees = 0; ``` to: ```jasvascript uint256 public totalFees = 0; ``` And update the line where `totalFees` is updated from: ```diff - totalFees = totalFees + uint64(fee); + totalFees = totalFees + fee; ``` This way, you ensure that the data types are consistent and can handle the range of values that your contract may encounter.
The contest is live. Earn rewards by submitting a finding.
Submissions are being reviewed by our AI judge. Results will be available in a few minutes.
View all submissionsThe contest is complete and the rewards are being distributed.