Beginner FriendlySolidity
100 EXP
View results
Submission Details
Severity: high
Invalid

Flawed Beneficiary Index Retrieval Function

Description

The _getBeneficiaryIndex() function contains a critical vulnerability in how it handles non-existent beneficiary addresses. The function is designed to return the index of a beneficiary in the beneficiaries array, but it fails to properly handle cases where the address is not found.

function _getBeneficiaryIndex(address _beneficiary) public view returns (uint256 _index) {
for (uint256 i = 0; i < beneficiaries.length; i++) {
if (_beneficiary == beneficiaries[i]) {
_index = i;
break;
}
}
}

The issue stems from the implicit initialization of the return variable _index to 0. When the function is called with an address that doesn't exist in the array, it completes the loop without finding a match, and therefore returns the default value of 0, which is indistinguishable from a successful lookup of the beneficiary at index 0.

Impact

This vulnerability has several significant consequences:

  1. False Positives: The function incorrectly suggests that a non-existent beneficiary is located at index 0, leading to confusion and potential misuse.

  2. Dependent Function Failures: Functions relying on _getBeneficiaryIndex() (such as removeBeneficiary()) will operate on the wrong array element when dealing with non-existent beneficiaries.

  3. Silent Failures: The issue doesn't trigger any errors or reverts, making it difficult to detect when non-existent beneficiaries are being processed.

  4. Critical Data Corruption: This can lead to unintended modification or deletion of the first beneficiary's data, potentially disrupting the entire inheritance structure.

  5. Previously deleted beneficiary returns index 0 when _getBeneficiaryIndex is called with the beneficiary.

Recommendation

The function should be modified to explicitly handle the case when a beneficiary is not found:

Use a revert mechanism

function _getBeneficiaryIndex(address _beneficiary) public view returns (uint256) {
for (uint256 i = 0; i < beneficiaries.length; i++) {
if (_beneficiary == beneficiaries[i]) {
return i;
}
}
revert("Beneficiary not found");
}

Tools Used

  • Manual Code Review

  • Foundry test suite

Updates

Lead Judging Commences

0xtimefliez Lead Judge 9 months 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!