++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.
Handle
Dravee
Vulnerability details
Impact
++i
costs less gas compared toi++
for unsigned integer, as pre-increment is cheaper (about 5 gas per iteration)Proof of Concept
i++
incrementsi
and returns the initial value ofi
. Which means:But
++i
returns the actual incremented value:In the first case, the compiler has to create a temporary variable (when used) for returning
1
instead of2
Instances include:
Tools Used
VS Code
Recommended Mitigation Steps
Use
++i
instead ofi++
to increment the value of an uint variable.