sherlock-audit / 2024-02-optimism-2024-judging

6 stars 4 forks source link

`DisputeGameFactory.findLatestGames` function code can be modified to improve efficiency #250

Closed sherlock-admin4 closed 7 months ago

sherlock-admin4 commented 7 months ago

DisputeGameFactory.findLatestGames function code can be modified to improve efficiency

Low/Info issue submitted by Shield

Summary

Vulnerability Detail

In the DisputeGameFactory.findLatestGames function the follwoing if statement is used to break the for loop in the event required number of _n latest games are retrieved from the _disputeGameList array as shown below:

                if (games_.length >= _n) break; 

But the _game.length start from 0 (before getting incremented) and will always equal n value before it exceeds the n value.

Impact

The >= check is unnecessary and makes the code inefficient.

Code Snippet

https://github.com/sherlock-audit/2024-02-optimism-2024/blob/main/optimism/packages/contracts-bedrock/src/dispute/DisputeGameFactory.sol#L179

Tool used

Manual Review

Recommendation

The >= can be updated as == to make the code more efficient since the _game.length start from 0 (before getting incremented) and will always equal n value before it exceeds the n value.

                if (games_.length == _n) break;