The _getBeneficiaryIndex function, designed to find the index of a beneficiary within the beneficiaries array, has a critical flaw: if the provided beneficiary address is not found in the array, the function will silently return 0. This can lead to incorrect behavior in the removeBeneficiary function, where it's used, potentially causing the wrong beneficiary to be removed or even modifying the state in an unexpected way.
_getBeneficiaryIndex Function:
Purpose: The function iterates through the beneficiaries array to find the index of a given _beneficiary address.
Logic: It loops through the array, and if a match is found, it sets the _index return variable to the current index (i) and breaks the loop.
Flaw: If no match is found, the loop completes without ever setting _index. Because _index is initialized as 0, the function will return 0 in this scenario.
Code:
removeBeneficiary Dependency:
Usage: The removeBeneficiary function directly uses the result of _getBeneficiaryIndex to determine which element to remove.
Issue: If _getBeneficiaryIndex incorrectly returns 0 when the beneficiary isn't found, removeBeneficiary will always try to remove the element at index 0 in the beneficiaries array, even if that's not the intended target.
Code:
Incorrect Removal: If an owner attempts to remove a beneficiary who is not actually in the beneficiaries array, the function will silently remove the beneficiary at index 0 instead.
Accidental Removal of First Beneficiary: This can lead to the unintended removal of the first added beneficiary, as index 0 will be wrongly targeted.
Manual Code Review
Return a Boolean and Update the Index: Modify _getBeneficiaryIndex to return a bool indicating success or failure and update _index as an output. This makes the function much more informative.
Update removeBeneficiary: Modify removeBeneficiary to check the new found return value from _getBeneficiaryIndex and handle the case where the beneficiary is not found.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.