20,000 USDC
View results
Submission Details
Severity: gas
Valid

Cache array length outside of loop

Summary

Cache array length outside of loop

Vulnerability Details

If not cached, the solidity compiler will always read the length of the array during each iteration (except the first iteration):

  • Storage arrays incur sload operation - 100 additional extra gas for each iteration

  • Memory arrays incur mload operation - 3 additional extra gas for each iteration

  • Calldata arrays incur CALLDATALOAD operation - 3 additional extra gas for each iteration

Caching the length changes each of these to a DUP<N> (3 gas), and gets rid of the extra DUP<N> needed to store the stack offset.

File: src/Lender.sol
280: loans.length - 1,

Link to code - https://github.com/Cyfrin/2023-07-beedle/tree/main/src/Lender.sol

Tools Used

Code Review

Recommendations

Cache array length outside of loop

Auditor

NeoCrao

Support

FAQs

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