code-423n4 / 2021-10-tally-findings

0 stars 0 forks source link

Unnecessary array boundaries check when loading an array element twice #72

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

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:

uint256 item = array[i];
// do something with `item`
// do some other thing with `item`