When you use the delete
keyword in the context of an array, deleting the variable from the list just initiates it to its default value, which in the context of beneficiaries[]
array will be the address(0)
. Deleting an element like this from an array, doesn't make the array shorter and if there's a beneficiary that has to be removed from the array, all the other beneficiaries that remain in the beneficiaries[]
array will not get the right amount of funds (in fact they would get less funds). Some of the funds will get burnt and sent to address(0)
, when a beneficiary calls InheritanceManager::withdrawInheritedFunds
.
Place the following snippet in your test suite, in InheritanceManagerTest.t.sol
.
As you can see, the beneficiaries that are left in the array are 2, but the array length is 3. So 1/3 of the funds will be burnt.
Some of the funds that should be inherited in the future, might be accidentally burnt.
Manual Review
There are 2 ways of properly deleting an element from an array.
Option 1: Shift all elements to remain in the array, that are placed after the one to be removed, to the left. Then pop the last element. This option keeps the correct order of the elements in the array.
Option 2: Replace the element to be removed with the last element of the array and then pop the last elements (as it would be doubled). This option doesn't keep the same order of the elements in the array.
Option 2 is the recommended choice here, because the order of the elements in the beneficiaries[]
array doesn't matter and it is a lot more gas efficient to use this method.
In InheritanceManager::removeBeneficiary
:
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.