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

Incorrect delete logic in the removeBeneficiary()

Summary

Delete only resets the element to its default value (e.g., address(0)) and does not reduce the array's length. This leaves gaps in the array, which can lead to inefficiencies and potential issues when iterating or relying on the array's length.

Vulnerability Details

  • The vulnerability lies in the removeBeneficiary function:

function removeBeneficiary(address _beneficiary) external onlyOwner {
uint256 indexToRemove = _getBeneficiaryIndex(_beneficiary);
delete beneficiaries[indexToRemove];
}
  • The delete keyword resets the element at indexToRemove to its default value but does not remove it from the array. This results in a gap (e.g., address(0)) in the beneficiaries array, and the array length remains unchanged.

Impact

  • The array may contain invalid entries (address(0)), which can cause issues in logic that relies on the array's contents.

  • The array length remains unchanged, leading to unnecessary storage usage and higher gas costs for operations involving the array.

  • Potential Bugs: Iterating over the array or relying on its length for calculations may produce unexpected results.

Tools Used

  • Manual code review

Recommendations

  1. Replace the element to be removed with the last element in the array.

  2. Use pop() to remove the last element and reduce the array's length.

function removeBeneficiary(address _beneficiary) external onlyOwner {
uint256 indexToRemove = _getBeneficiaryIndex(_beneficiary);
require(indexToRemove < beneficiaries.length, "Beneficiary not found");
// Replace the element to remove with the last element
beneficiaries[indexToRemove] = beneficiaries[beneficiaries.length - 1];
// Remove the last element using pop()
beneficiaries.pop();
}
Updates

Lead Judging Commences

0xtimefliez Lead Judge 6 months ago
Submission Judgement Published
Validated
Assigned finding tags:

Incorrect removal from beneficiary list causes funds to be send to 0 address

Support

FAQs

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