Beginner FriendlyFoundryNFT
100 EXP
View results
Submission Details
Severity: medium
Valid

enterRaffle() can result in "Out of Gas" error

Summary

enterRaffle() consumes a lot of gas which can result in DoS and "out of gas" error.

Vulnerability Details

entherRaffle() uses loops and nested loops in for iterating the players array. This results in longer function execution and potential out of gas problem. The test will not revert because Foundry has a very high gas limit. The following:

function testCanEnterRaffleRunOutOfGas() public {
uint64 playerNum = 64;
address[] memory players = new address[](playerNum);
for (uint64 i = 0; i < playerNum; i++) {
players[i] = address(i);
}
puppyRaffle.enterRaffle{value: entranceFee * playerNum}(players);
assertEq(puppyRaffle.players(0), address(0));
assertEq(puppyRaffle.players(playerNum - 1), address(playerNum - 1));
}

shows how quickly used gas is increased:

|number of players|gas used|
----------------------------
| 2| 68900|
| 16| 491158|
| 32| 1163116|
| 64| 3113002|

Impact

High. The attacker may easily discover this vulnerability and perform a denial-of-service attack because the function takes a lot of time to iterate through the loops.

Tools Used

Manual check.

Recommendations

Avoid loops and especially nested loops. Refactor the contract to use a mapping(uint256 => address) players instead of an array.

Updates

Lead Judging Commences

Hamiltonite Lead Judge about 2 years ago
Submission Judgement Published
Validated
Assigned finding tags:

denial-of-service-in-enter-raffle

Support

FAQs

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

Give us feedback!