Closed code423n4 closed 2 years ago
Dravee
Increased gas cost
(As seen on this contest: https://github.com/code-423n4/2021-10-slingshot-findings/issues/63) Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.
Caching the array length in the stack saves around 3 gas per iteration.
Instances include:
RebalanceManager.sol#rebalance()
RebalanceManagerV2.sol#rebalance()
RebalanceManagerV3.sol#rebalance()
BasketFacet.sol#removeToken
BasketFacet.sol#joinPool
BasketFacet.sol#exitPool
BasketFacet.sol#getTokens
BasketFacet.sol#calcTokensForAmount
BasketFacet.sol#calcTokensForAmountExit
CallFacet.sol#call
CallFacet.sol#removeCaller
CallFacet.sol#callNoValue
SingleNativeTokenExit.sol#exitEth
SingleNativeTokenExitV2.sol#_exit
SingleTokenJoin.sol#_joinTokenSingle
SingleTokenJoinV2.sol#_joinTokenSingle
VS Code
Store the array's length in a variable before the for-loop, and use it instead.
Duplicate of #249
Handle
Dravee
Vulnerability details
Impact
Increased gas cost
Proof of Concept
(As seen on this contest: https://github.com/code-423n4/2021-10-slingshot-findings/issues/63) Reading array length at each iteration of the loop takes 6 gas (3 for mload and 3 to place memory_offset) in the stack.
Caching the array length in the stack saves around 3 gas per iteration.
Instances include:
RebalanceManager.sol#rebalance()
https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/callManagers/RebalanceManager.sol#L218 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/callManagers/RebalanceManager.sol#L234RebalanceManagerV2.sol#rebalance()
https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/callManagers/RebalanceManagerV2.sol#L155RebalanceManagerV3.sol#rebalance()
https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/callManagers/RebalanceManagerV3.sol#L166 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/callManagers/RebalanceManagerV3.sol#L171BasketFacet.sol#removeToken
,BasketFacet.sol#joinPool
,BasketFacet.sol#exitPool
,BasketFacet.sol#getTokens
,BasketFacet.sol#calcTokensForAmount
,BasketFacet.sol#calcTokensForAmountExit
https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/facets/Basket/BasketFacet.sol#L50 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/facets/Basket/BasketFacet.sol#L160 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/facets/Basket/BasketFacet.sol#L202 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/facets/Basket/BasketFacet.sol#L321 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/facets/Basket/BasketFacet.sol#L348 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/facets/Basket/BasketFacet.sol#L381CallFacet.sol#call
,CallFacet.sol#removeCaller
,CallFacet.sol#callNoValue
https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/facets/Call/CallFacet.sol#L55 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/facets/Call/CallFacet.sol#L82 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/facets/Call/CallFacet.sol#L95SingleNativeTokenExit.sol#exitEth
https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/singleJoinExit/SingleNativeTokenExit.sol#L69SingleNativeTokenExitV2.sol#_exit
https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/singleJoinExit/SingleNativeTokenExitV2.sol#L74 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/singleJoinExit/SingleNativeTokenExitV2.sol#L76SingleTokenJoin.sol#_joinTokenSingle
https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/singleJoinExit/SingleTokenJoin.sol#L108SingleTokenJoinV2.sol#_joinTokenSingle
https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/singleJoinExit/SingleTokenJoinV2.sol#L86 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/singleJoinExit/SingleTokenJoinV2.sol#L91 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/singleJoinExit/SingleTokenJoinV2.sol#L100 https://github.com/code-423n4/2021-12-amun/blob/main/contracts/basket/contracts/singleJoinExit/SingleTokenJoinV2.sol#L117Tools Used
VS Code
Recommended Mitigation Steps
Store the array's length in a variable before the for-loop, and use it instead.