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

1 stars 0 forks source link

Gas in `ConvexStakingWrapper.sol:addRewards()`: `startIndex` can't underflow #62

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

Dravee

Vulnerability details

Impact

Increased gas cost due to unnecessary automatic underflow checks.

Solidity version 0.8+ comes with implicit overflow and underflow checks on unsigned integers.

When an overflow or an underflow isn't possible (as an example, when a comparison is made before the arithmetic operation, or the operation doesn't depend on user input), some gas can be saved by using an unchecked block.

https://docs.soliditylang.org/en/v0.8.10/control-structures.html#checked-or-unchecked-arithmetic

Proof of Concept

In ConvexStakingWrapper.sol:addRewards(), rewardsLength has a minimum value of 1:

File: ConvexStakingWrapper.sol
104:         uint256 rewardsLength = rewards.length;
105: 
106:         if (rewardsLength == 0) {
107:             RewardType storage reward = rewards.push();
108:             reward.reward_token = crv;
109:             reward.reward_pool = mainPool;
110:             rewardsLength += 1;
111:         }

Therefore, this line can't underflow:

File: ConvexStakingWrapper.sol
114:         uint256 startIndex = rewardsLength - 1; //@audit-info can't underflow as rewardsLength is >= 1

This should be inside an unchecked block.

Tools Used

VS Code

Recommended Mitigation Steps

Uncheck arithmetic operations when the risk of underflow or overflow is already contained by wrapping them in an unchecked block

devtooligan commented 2 years ago

Good catch, I guess you could argue that rewardsLength can't realistically overflow either

GalloDaSballo commented 2 years ago

Leaving https://github.com/code-423n4/2022-01-yield-findings/issues/67 valid but marking the rest as invalid as they are duplicates by the same warden