players.length After RefundsTitle: Incorrect Fee and Prize Calculation Using players.length After Refunds
Impact: High
Likelihood: Medium
The selectWinner function calculates the total collected amount and distributes 80% to the winner and 20% as fees. This calculation should reflect the actual number of active, non-refunded players who have paid entrance fees.
The calculation multiplies players.length by entranceFee to determine the total amount collected. However, when a player calls refund, their slot is set to address(0) but remains in the array, meaning players.length does not decrease. This causes the contract to calculate fees and prize amounts based on the original array size rather than the actual number of active participants who still have funds in the contract.
Likelihood:
Any player can call refund to withdraw their entrance fee, creating a gap between the actual contract balance and the calculated totalAmountCollected. Every time a player refunds, the discrepancy grows by one entranceFee.
The issue manifests every time selectWinner is called after one or more refunds have occurred. No special attacker action is needed beyond normal user behavior — any participant who requests a refund triggers the miscalculation.
Impact:
The prizePool value is calculated based on the inflated totalAmountCollected, meaning the winner may receive ETH that was never actually deposited by active players. This effectively redistributes funds from other legitimate participants or the fee pool to the winner unfairly.
The totalFees counter accumulates an inflated value based on players.length, which means withdrawFees may attempt to withdraw more fees than the contract actually holds. The withdrawFees function has a guard (require(address(this).balance == uint256(totalFees))), but because the balance is lower than totalFees, the owner will be permanently unable to withdraw accumulated fees.
This PoC demonstrates the cascading failure caused by the miscalculation. After one player refunds, the contract holds 3 ETH but players.length remains 4. When selectWinner calculates the prize pool as 80% of 4 ETH (3.2 ETH), it exceeds the actual contract balance of 3 ETH. The winner.call{value: prizePool} either reverts (blocking the entire raffle) or succeeds with a lower amount due to address(this).balance being insufficient. Meanwhile, totalFees is set to 0.8 ETH, but since withdrawFees requires address(this).balance == totalFees, the owner can never withdraw because the balances no longer match.
Using address(this).balance instead of players.length * entranceFee ensures the fee and prize calculations are based on the actual ETH held by the contract. After refunds are paid out, the remaining balance accurately represents the distributable funds. This also fixes the withdrawFees guard condition, since totalFees will now match the actual contract balance after a successful selectWinner call.
## Description `PuppyRaffle::refund` is supposed to refund a player and remove him from the current players. But instead, it replaces his index value with address(0) which is considered a valid value by solidity. This can cause a lot issues because the players array length is unchanged and address(0) is now considered a player. ## Vulnerability Details ```javascript players[playerIndex] = address(0); @> uint256 totalAmountCollected = players.length * entranceFee; (bool success,) = winner.call{value: prizePool}(""); require(success, "PuppyRaffle: Failed to send prize pool to winner"); _safeMint(winner, tokenId); ``` If a player refunds his position, the function `PuppyRaffle::selectWinner` will always revert. Because more than likely the following call will not work because the `prizePool` is based on a amount calculated by considering that that no player has refunded his position and exit the lottery. And it will try to send more tokens that what the contract has : ```javascript uint256 totalAmountCollected = players.length * entranceFee; uint256 prizePool = (totalAmountCollected * 80) / 100; (bool success,) = winner.call{value: prizePool}(""); require(success, "PuppyRaffle: Failed to send prize pool to winner"); ``` However, even if this calls passes for some reason (maby there are more native tokens that what the players have sent or because of the 80% ...). The call will thankfully still fail because of the following line is minting to the zero address is not allowed. ```javascript _safeMint(winner, tokenId); ``` ## Impact The lottery is stoped, any call to the function `PuppyRaffle::selectWinner`will revert. There is no actual loss of funds for users as they can always refund and get their tokens back. However, the protocol is shut down and will lose all it's customers. A core functionality is exposed. Impact is high ### Proof of concept To execute this test : forge test --mt testWinnerSelectionRevertsAfterExit -vvvv ```javascript function testWinnerSelectionRevertsAfterExit() public playersEntered { vm.warp(block.timestamp + duration + 1); vm.roll(block.number + 1); // There are four winners. Winner is last slot vm.prank(playerFour); puppyRaffle.refund(3); // reverts because out of Funds vm.expectRevert(); puppyRaffle.selectWinner(); vm.deal(address(puppyRaffle), 10 ether); vm.expectRevert("ERC721: mint to the zero address"); puppyRaffle.selectWinner(); } ``` ## Recommendations Delete the player index that has refunded. ```diff - players[playerIndex] = address(0); + players[playerIndex] = players[players.length - 1]; + players.pop() ```
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.