byterocket / c4-common-issues

A collection of common security issues and possible gas optimizations in solidity smart contracts

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

pmerkleplant opened this issue · comments

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

Severity

Which severity would you assign to this Issue?

  • Gas Optimization
  • Non-Critical
  • 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