Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache array length outside of loop #86

Closed
code423n4 opened this issue Dec 16, 2021 · 1 comment
Closed

Cache array length outside of loop #86

code423n4 opened this issue Dec 16, 2021 · 1 comment
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization)

Comments

@code423n4
Copy link
Contributor

Handle

pmerkleplant

Vulnerability details

Impact

Caching the array length outside a loop in a variable saves reading it on each
iteration as long as the array's length is not changed during the loop.

Therefore, the following loops could be refactored:

./callManagers/RebalanceManagerV2.sol:155: for (uint256 i; i < _swapsV2.length; i++)
./factories/PieFactoryContract.sol:88: for (uint256 i = 0; i < _tokens.length; i++)
./callManagers/RebalanceManagerV3.sol:166: for (uint256 i; i < _swapsV2.length; i++)
./callManagers/RebalanceManagerV3.sol:171: for (uint256 j; j < trade.swaps.length; j++)
./facets/Call/CallFacet.sol:55: for (uint256 i = 0; i < callStorage.callers.length; i++)
./facets/Call/CallFacet.sol:82: for (uint256 i = 0; i < _targets.length; i++)
./facets/Call/CallFacet.sol:95: for (uint256 i = 0; i < _targets.length; i++)
./singleJoinExit/SingleNativeTokenExitV2.sol:74: for (uint256 i; i < _exitTokenStruct.trades.length; i++)
./singleJoinExit/SingleNativeTokenExitV2.sol:76: for (uint256 j; j < trade.swaps.length; j++)
./singleJoinExit/SingleTokenJoin.sol:108: for (uint256 i; i < tokens.length; i++)
./singleJoinExit/SingleTokenJoinV2.sol:86: for (uint256 i; i < _joinTokenStruct.trades.length; i++)
./singleJoinExit/SingleTokenJoinV2.sol:91: for (uint256 j; j < trade.swaps.length; j++)
./singleJoinExit/SingleTokenJoinV2.sol:100: for (uint256 j; j < trade.swaps.length; j++)
./singleJoinExit/SingleTokenJoinV2.sol:117: for (uint256 i; i < tokens.length; i++)
./callManagers/RebalanceManager.sol:218: for (uint256 i; i < _swapsV2.length; i++)
./callManagers/RebalanceManager.sol:234: for (uint256 i; i < _swapsV3.length; i++)
./singleJoinExit/SingleNativeTokenExit.sol:69: for (uint256 i; i < tokens.length; i++)
./facets/Basket/BasketFacet.sol:50: for (uint256 i; i < bs.tokens.length; i++) (changes the array size, but breaks afterwards)
./facets/Basket/BasketFacet.sol:160: for (uint256 i; i < bs.tokens.length; i++)
./facets/Basket/BasketFacet.sol:202: for (uint256 i; i < bs.tokens.length; i++)
./facets/Basket/BasketFacet.sol:321: for (uint256 i = 0; i < tokens.length; i++)
./facets/Basket/BasketFacet.sol:348: for (uint256 i; i < bs.tokens.length; i++)
./facets/Basket/BasketFacet.sol:381: for (uint256 i; i < bs.tokens.length; i++)

Recommended Mitigation Steps

Use the following pattern for refactoring:

uint length = arr.length;
for (uint i; i < length; i++) {
    // ...
}

Tools used

grep

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Dec 16, 2021
code423n4 added a commit that referenced this issue Dec 16, 2021
@ghost ghost added the duplicate This issue or pull request already exists label Dec 30, 2021
@ghost ghost closed this as completed Jan 10, 2022
@0xleastwood
Copy link
Collaborator

Duplicate of #249

@0xleastwood 0xleastwood marked this as a duplicate of #249 Jan 23, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists G (Gas Optimization)
Projects
None yet
Development

No branches or pull requests

2 participants