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

Optimize validateWeights #213

Closed
code423n4 opened this issue Sep 22, 2021 · 2 comments
Closed

Optimize validateWeights #213

code423n4 opened this issue Sep 22, 2021 · 2 comments
Labels
bug Warden finding duplicate Another warden found this issue G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

pauliax

Vulnerability details

Impact

I assume function validateWeights is very gas-consuming as it iterates against all the tokens again and again and re-assigns dynamic arrays multiple times. I think a good improvement that you may consider would be to ask the _tokens array to be provided in descending order, then a validation would be much cheaper.

Recommended Mitigation Steps

An example solution:

// tokens must be in a descending order
function validateWeights(address[] memory _tokens, uint256[] memory _weights) public pure {
    uint256 length = _tokens.length;
    require(length == _weights.length);
    uint lastIndex = length - 1;
    // check uniqueness of tokens and not token(0)
    for (uint i = 0; i < length; i++) {
        require(_tokens[i] != address(0));
        require(_weights[i] > 0);
        if (i != lastIndex) {
           require(_tokens[i] > _tokens[i + 1], "descending");
        }
    }
}
code423n4 added a commit that referenced this issue Sep 22, 2021
@frank-beard frank-beard added duplicate Another warden found this issue sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") labels Oct 19, 2021
@frank-beard
Copy link
Collaborator

#226

@GalloDaSballo
Copy link
Collaborator

Duplicate of #160

@GalloDaSballo GalloDaSballo marked this as a duplicate of #160 Nov 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Warden finding duplicate Another warden found this issue G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

3 participants