code-423n4 / 2021-11-badgerzaps-findings

0 stars 0 forks source link

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

Closed code423n4 closed 2 years ago

code423n4 commented 3 years ago

Handle

pants

Vulnerability details

At IbbtcVaultZap.deposit you read _amounts[i] twice.

Vulnerability details

There are many places in the code (especially in for-each loops) that loads the same array element 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`
GalloDaSballo commented 3 years ago

Disagree with the finding, we are reading from calldata which means cost of reading is 3 gas per read, cost of storing on storage is 3 as well, it would give is no savings to apply this improvemnet

0xleastwood commented 2 years ago

Agree with sponsor, making finding invalid.