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

0 stars 0 forks source link

Avoid unnecessary read of array length in for loops can save gas #36

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

WatchPug

Vulnerability details

Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.

https://github.com/Badger-Finance/ibbtc/blob/d8b95e8d145eb196ba20033267a9ba43a17be02c/contracts/Zap.sol#L70-L80

for (uint i = 0; i < pools.length; i++) {
    Pool memory pool = pools[i];
    pool.lpToken.safeApprove(address(pool.sett), uint(-1));
    if (i < 3) {
        ren.safeApprove(address(pool.deposit), uint(-1));
        wbtc.safeApprove(address(pool.deposit), uint(-1));
        IERC20(address(pool.sett)).safeApprove(address(settPeak), uint(-1));
    } else {
        IERC20(address(pool.sett)).safeApprove(address(byvWbtcPeak), uint(-1));
    }
}

As pools.length must be 4, it can be replaced with a literal 4.

GalloDaSballo commented 2 years ago

Agree with the finding as the warden actually explained the logic