The function Swap.sweepFees() loads the same array element (tokens[i]) more than once. In such cases, only one array boundaries check should take place, and the rest are unnecessary. Therefore, this array element should be cached in a local variable and then be loaded again using this local variable, skipping the redundent second array boundaries check.
Impact
Unnecessary array boundaries checks are taken place and increase gas costs.
Tool Used
Manual code review.
Recommended Mitigation Steps
Load the array elements once, cache them in local variables and then read them again using the local variables. For example:
uint256 item = array[i];
// do something with `item`
// do some other thing with `item`
Handle
pants
Vulnerability details
The function
Swap.sweepFees()
loads the same array element (tokens[i]
) more than once. In such cases, only one array boundaries check should take place, and the rest are unnecessary. Therefore, this array element should be cached in a local variable and then be loaded again using this local variable, skipping the redundent second array boundaries check.Impact
Unnecessary array boundaries checks are taken place and increase gas costs.
Tool Used
Manual code review.
Recommended Mitigation Steps
Load the array elements once, cache them in local variables and then read them again using the local variables. For example: