code-423n4 / 2022-01-yield-findings

1 stars 0 forks source link

Avoid unnecessary arithmetic operations and storage reads can save gas #101

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

WatchPug

Vulnerability details

https://github.com/code-423n4/2022-01-yield/blob/e946f40239b33812e54fafc700eb2298df1a2579/contracts/ConvexStakingWrapper.sol#L106-L111

    if (rewardsLength == 0) {
        RewardType storage reward = rewards.push();
        reward.reward_token = crv;
        reward.reward_pool = mainPool;
        rewardsLength += 1;
    }

When rewardsLength == 0, the new rewardsLength will always be 1. Therefore, replacing += with = can avoid the unnecessary arithmetic operations and memory reads

Recommendation

Change to:

    if (rewardsLength == 0) {
        RewardType storage reward = rewards.push();
        reward.reward_token = crv;
        reward.reward_pool = mainPool;
        rewardsLength = 1;
    }
GalloDaSballo commented 2 years ago

Finding is valid and the sponsor used it, notice that this should save 3 gas as it's avoiding an MLOAD