c4-common-issues icon indicating copy to clipboard operation
c4-common-issues copied to clipboard

Hidden unbounded loop in function call due to storage to memory copy

Open pmerkleplant opened this issue 2 years ago • 0 comments

Hidden unbounded loop in function call due to storage to memory copy

Severity

Which severity would you assign to this Issue?

  • [X] Gas Optimization
  • [ ] Non-Critical
  • [X] Low Risk
  • [ ] Med Risk
  • [ ] High Risk

Description

See https://twitter.com/danielvf/status/1519381832592199681.

Example

If applicable, provide a small example. If not, delete this section.

🤦 Bad:

unit[] public array;

function _requireLength(unit[] memory _array) internal pure {
    // The memory keyword leads to SLOADing the entire array into memory, i.e. creates hidden, unbounded loop.
    require(_array.length > 0, "Empty array");
}

🚀 Good:

unit[] public array;

function _requireLength(unit[] storage _array) internal pure {
    // No hidden loop.
    require(_array.length > 0, "Empty array");
}

Background Information

See https://twitter.com/danielvf/status/1519381832592199681.

Code4Rena Report/Issue Link

TODO: Not found yet

pmerkleplant avatar May 19 '22 15:05 pmerkleplant