Mystery Box

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

Potential "index out of bounds" in `claimSingleReward()`

Summary

In the claimSingleReward() function, an index check is performed before using _index. However, this check does not adequately prevent "index out of bounds" errors.

Vulnerability Details

The expression require(_index <= rewardsOwned[msg.sender].length, "Invalid index"); checks whether _index is less than or equal to the length of the rewardsOwned[msg.sender] array. However, array indices in Solidity are zero-based, meaning the valid indices for an array of length n range from 0 to n - 1.

Thus, allowing _index to be equal to rewardsOwned[msg.sender].length is incorrect, as it would point to an invalid array position, leading to an "index out of bounds" error when accessing the array.

Impact

  1. Reverted Transactions: If _index == rewardsOwned[msg.sender].length, the contract will try to access an invalid array index, causing the transaction to revert with an "index out of bounds" error.

  2. User Frustration: Users could encounter frequent transaction failures when they attempt to claim rewards, especially when they provide an index that equals the length of the array, resulting in a poor user experience.

Tools Used

Manual review

Recommendations

function claimSingleReward(uint256 _index) public {
- require(_index <= rewardsOwned[msg.sender].length, "Invalid index");
+ require(_index < rewardsOwned[msg.sender].length, "Invalid index");
uint256 value = rewardsOwned[msg.sender][_index].value;
require(value > 0, "No reward to claim");
(bool success,) = payable(msg.sender).call{value: value}("");
require(success, "Transfer failed");
delete rewardsOwned[msg.sender][_index];
}
Updates

Appeal created

inallhonesty Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity

Support

FAQs

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

Give us feedback!