Skip to content

Caching array length to save gas #123

Open
@code423n4

Description

@code423n4

Handle

pants

Vulnerability details

You can cache the array length to save length access gas cost at every iteration.
The following (bad) form appears at GovernorAlpha.sol line 571 and LinearVesting.sol line 72.
GovernorAlpha.sol example:

        for (uint256 i = 0; i < _targets.length; i++) {
            if (_targets[i] == address(this)) {
                revert(
                    "GovernorAlpha::veto: council cannot veto on proposal having action with address(this) as target"
                );
            }
        }

Change it to:

        uint256 len = _targets.length;
        for (uint256 i = 0; i < len; i++) {
            if (_targets[i] == address(this)) {
                revert(
                    "GovernorAlpha::veto: council cannot veto on proposal having action with address(this) as target"
                );
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Labels

    G (Gas Optimization)bugSomething isn't workingsponsor acknowledgedTechnically the issue is correct, but we're not going to resolve it for XYZ reasons

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions