Closed code423n4 closed 1 year ago
not a bad idea, I will just reverse the logic and make the for loop start from the end of the array
I agree with the issue; but because it's a view function that isn't called internally nor by other contracts in scope, there is no impact on funds.
Hence, the issue should be QA (also because it would take quite a number of epochs before gas limit usage is exceeded).
part of #463
Lines of code
https://github.com/code-423n4/2022-09-y2k-finance/blob/main/src/Vault.sol#L438-L451 https://github.com/code-423n4/2022-09-y2k-finance/blob/main/src/Vault.sol#L430-L432 https://github.com/code-423n4/2022-09-y2k-finance/blob/main/src/Vault.sol#L307-L322
Vulnerability details
Impact
In
Vault.sol
, if theepochs
array grows very large over time, callinggetNextEpoch()
might consume more gas than the block limit.Proof of Concept
getNextEpoch
will loop throughepochs.length
https://github.com/code-423n4/2022-09-y2k-finance/blob/main/src/Vault.sol#L438-L451
https://github.com/code-423n4/2022-09-y2k-finance/blob/main/src/Vault.sol#L430-L432
For each
createAssets()
call, a new epoch will be pushed into the arrayhttps://github.com/code-423n4/2022-09-y2k-finance/blob/main/src/Vault.sol#L307-L322
Recommended Mitigation Steps
There's three possible solutions
epochs
array from getting too largeepochs
is garanteed to be sorted, it's possible to use binary serch instead of linear search to retrieve the next epoch ingetNextEpoch()
startIndex
and aendIndex
to avoid having to iterate all the items ingetNextEpoch()
. e.g. a frontend could call this function with different start and end indexes only if calling from 0 to length - 1 results in an out-of-gas error.