Platform: Code4rena
Start Date: 08/12/2021
Pot Size: $30,000 ETH
Total HM: 12
Participants: 26
Period: 3 days
Judge: leastwood
Total Solo HM: 9
Id: 65
League: ETH
Rank: 14/26
Findings: 2
Award: $44.98
🌟 Selected for report: 1
🚀 Solo Findings: 0
🌟 Selected for report: 0x0x0x
Also found by: Jujic, Meta0xNull, WatchPug, pmerkleplant, rishabh
Meta0xNull
There are many Functions loops that follows this for-each pattern: for (uint256 i = 0; i < weights.length; i++) { // do something }
In such for loops, the weights.length is read from Storage on every iteration, instead of caching it once in a local variable and read it again using the local variable.
Storage reads are a bit more expensive than reading local variables.
https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Basket.sol#L275 https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Basket.sol#L282 https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Basket.sol#L290
Manual Review
Read these values from storage once, cache them in local variables and then read them again using the local variables. For example:
uint256 weights_temp = weights; for (uint256 i = 0; i < weights_temp.length; i++) { // do something // Replace weights[i] to weights_temp[i] }
#0 - 0xleastwood
2022-03-27T10:30:28Z
Duplicate of #140
🌟 Selected for report: Meta0xNull
Meta0xNull
The local variable used as for loop index need not be initialized to 0 because the default value is 0. Avoiding this anti-pattern can save a few opcodes and therefore a tiny bit of gas.
https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Basket.sol#L75 https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Basket.sol#L79 https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Basket.sol#L275 https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Basket.sol#L282 https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Basket.sol#L289 https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Factory.sol#L109 https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Auction.sol#L88 https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Auction.sol#L92 https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Auction.sol#L105 https://github.com/code-423n4/2021-12-defiprotocol/blob/main/contracts/contracts/Auction.sol#L152
Manual Review
Remove explicit 0 initialization of for loop index variable.
Before: for (uint256 i = 0; After for (uint256 i;