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

1 stars 0 forks source link

Adding unchecked directive can save gas #105

Open code423n4 opened 2 years ago

code423n4 commented 2 years ago

Handle

WatchPug

Vulnerability details

For the arithmetic operations that will never over/underflow, using the unchecked directive (Solidity v0.8 has default overflow/underflow checks) can save some gas from the unnecessary internal over/underflow checks.

For example:

  1. https://github.com/code-423n4/2022-01-yield/blob/e946f40239b33812e54fafc700eb2298df1a2579/contracts/ConvexStakingWrapper.sol#L114-L114
uint256 startIndex = rewardsLength - 1;

rewardsLength - 1 will never underflow.

  1. https://github.com/code-423n4/2022-01-yield/blob/e946f40239b33812e54fafc700eb2298df1a2579/contracts/ConvexYieldWrapper.sol#L82-L85
bool isLast = i == vaultsLength - 1;
if (!isLast) {
    vaults_[i] = vaults_[vaultsLength - 1];
}
iamsahu commented 2 years ago

From readme:

GalloDaSballo commented 2 years ago

Interestingly enough, while the specific link mentioned by the warden wouldn't save 100 gas, using unchecked on that code and the for loop would.

While I'm not a fan of unchecked due to syntax, I think the finding is valid and above the sponsors require of at last 100 gas

alcueca commented 2 years ago

Fair enough, thank you.