Mystery Box

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

Incorrect Index Check in `claimSingleReward` Function

Summary

The claimSingleReward function contains an incorrect index validation check. This flaw could potentially lead to out-of-bounds access, causing unexpected behavior or errors when users attempt to claim rewards.

Vulnerability Details

The issue occurs in the claimSingleReward function of the smart contract. The function uses an incorrect condition to check whether the provided index _index is within bounds of 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];

The issue arises from the <= comparison operator, which allows the index to be equal to the length of the array. In Solidity, arrays are zero-indexed, meaning that the valid indices for an array with n elements range from 0 to n - 1. If _index is equal to rewardsOwned[msg.sender].length, it will be out of bounds, leading to potential errors when accessing the array.

For example, if rewardsOwned[msg.sender] has a length of 3, the valid indices would be 0, 1, and 2. The current check would allow _index to be 3, which is outside the valid range and could cause a transaction failure.

Impact

Users may encounter runtime errors when attempting to claim a reward at an invalid index, disrupting the user experience.

Tools Used

Manual Review

Recommendations

To mitigate this vulnerability, the contract should use the < operator instead of <= to ensure that the index is strictly less than the length of the array. This will prevent out-of-bounds access and ensure the index is valid for array operations.

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!