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

1 stars 0 forks source link

Gas: `++i` costs less gas compared to `i++` #55

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

Dravee

Vulnerability details

Impact

++i costs less gas compared to i++ for unsigned integer, as pre-increment is cheaper (about 5 gas per iteration)

Proof of Concept

i++ increments i and returns the initial value of i. Which means:

uint i = 1;  
i++; // == 1 but i == 2  

But ++i returns the actual incremented value:

uint i = 1;  
++i; // == 2 and i == 2 too, so no need for a temporary variable  

In the first case, the compiler has to create a temporary variable (when used) for returning 1 instead of 2

Instances include:

ConvexStakingWrapper.sol:115:        for (uint256 i = startIndex; i < extraCount; i++) {
ConvexStakingWrapper.sol:172:        for (uint256 u = 0; u < accountsLength; u++) {
ConvexStakingWrapper.sol:227:        for (uint256 u = 0; u < accountsLength; u++) {
ConvexStakingWrapper.sol:271:        for (uint256 i = 0; i < rewardCount; i++) {
ConvexStakingWrapper.sol:287:        for (uint256 i = 0; i < rewardCount; i++) {
ConvexStakingWrapper.sol:315:        for (uint256 i = 0; i < rewardCount; i++) {
ConvexYieldWrapper.sol:63:        for (uint256 i = 0; i < vaultsLength; i++) {
ConvexYieldWrapper.sol:80:            for (uint256 i = 0; i < vaultsLength; i++) {
ConvexYieldWrapper.sol:111:        for (uint256 i = 0; i < userVaultLength; i++) {

Tools Used

VS Code

Recommended Mitigation Steps

Use ++i instead of i++ to increment the value of an uint variable.

alcueca commented 2 years ago

Duplicate of #14