code-423n4 / 2021-12-defiprotocol-findings

0 stars 0 forks source link

In `validateWeights` cache `_tokens[i]` #148

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

0x0x0x

Vulnerability details

The for loop inside validateWeights is as follows:

for (uint i = 0; i < length; i++) {
            require(_tokens[i] != address(0));
            require(_weights[i] > 0);

            for (uint256 x = 0; x < tokenList.length; x++) {
                require(_tokens[i] != tokenList[x]);
            }

            tokenList[i] = _tokens[i];
}

_tokens[i] is used three times at each iteration. Gas can be saved by caching _tokens[i].

0xleastwood commented 2 years ago

_tokens is already loaded in as memory. This does not save gas.