Mystery Box

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

Invalid Index Check in `MysteryBox.sol#claimSingleReward`

Description:
In the claimSingleReward() function of the MysteryBox contract, the condition checking for the validity of the _index is incorrect. The current check allows out-of-bounds access to the rewardsOwned array.

function claimSingleReward(uint256 _index) public {
|> 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];
}

In Solidity, arrays are 0-based, meaning the valid index range is from 0 to rewardsOwned[msg.sender].length - 1. The current condition require(_index <= rewardsOwned[msg.sender].length, "Invalid index"); incorrectly allows _index == rewardsOwned[msg.sender].length, which would result in out-of-bounds access.

Impact:
This could cause a runtime error due to accessing an invalid index, potentially leading to the function reverting unexpectedly and causing issues for users trying to claim rewards.

Recommendation:

Change the condition to ensure the index is strictly less than the length of the array, as the array indices range from 0 to length - 1.

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];
}

This fix will ensure that only valid indices are used, preventing any out-of-bounds errors when accessing the rewardsOwned array.

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!