15,000 USDC
View results
Submission Details
Severity: gas

Array length looked up in very loop

Summary

You can cache array lengths used in for loops

Vulnerability Details

The for loops in the contracts do not cache the array lengths but reread them from storage everytime.
This implies every iteration the length of array is read from storage which increases gas costs rather than a cached value that is read from memory

Impact

Gas Savings: Rereading a value form storage incurs 100 gas wheres from memory 3 gas. These costs add up as in each iteration 100 gas for storage is used up to reread array length that could have just been read once and cached in memory.

Tools Used

Manual Analysis

Recommendations

  1. DSCEngine.sol line 118 loop for length part can be rewritten as (Not that key as read from memory)
    uint256 _len = tokenAddresses.length
    for (uint256 i = 0; i < _len;) {....etc

  2. DSCEngine.sol line 118 loop for length part can be rewritten as (This save as lot of gas as read from storage)
    uint256 _len = s_collateralTokens.length
    for (uint256 i = 0; i < _len;) {....etc

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.