Mystery Box

First Flight #25
Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: low
Invalid

[EVMN] Element Deletion Method Could Waste Gas

Summary

Deleting an element in Solidity does not change the array length but only reset the element's value.

Vulnerability Details

The code on line 76 below deletes an element from rewardsOwned[msg.sender] array.

delete rewardsOwned[msg.sender][_index];

After the deletion, the array length stays the same. This means that the iteration on line 81-83 processes empty elements that have been deleted. This wastes gas from storage read operation and addition with empty/zero value.

for (uint256 i = 0; i < rewardsOwned[msg.sender].length; i++) {
totalValue += rewardsOwned[msg.sender][i].value;
}

A similar issue is also found on line 100.

Impact

Wasted gas when processing empty element.

Tools Used

Manual review.

Recommendations

Consider swapping the element to be deleted with the last element then pop the last element.

Updates

Appeal created

inallhonesty Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Too generic

Support

FAQs

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

Give us feedback!