code4rena-2022-08-nounsdao-g10
[G‑10] array.length should not be looked up in every loop of a for-loop
Summary
for 루프 조건문에 storage array의 <array>.length를 사용하는 것을 지적했다. 지역변수에 length를 저장한 뒤 이용하라 제안한다.
Keyword
gas optimization, storage, memory
Vulnerability
File: contracts/governance/NounsDAOLogicV1.sol
281: for (uint256 i = 0; i < proposal.targets.length; i++) {
319: for (uint256 i = 0; i < proposal.targets.length; i++) {
346: for (uint256 i = 0; i < proposal.targets.length; i++) {
371: for (uint256 i = 0; i < proposal.targets.length; i++) {File: contracts/governance/NounsDAOLogicV2.sol
292: for (uint256 i = 0; i < proposal.targets.length; i++) {
330: for (uint256 i = 0; i < proposal.targets.length; i++) {
357: for (uint256 i = 0; i < proposal.targets.length; i++) {
382: for (uint256 i = 0; i < proposal.targets.length; i++) {for 루프를 돌 때, 조건문에 storage array의 <array>.length를 매번 사용하지 말고 지역변수에 length를 저장한 뒤 이용하라 제안한다.
- storage array의 length를 이용하는 경우 Gwarmaccess (100 gas)
- memory array의 length를 이용하는 경우 MLOAD (3 gas)
- calldata array의 length를 이용하는 경우 CALLDATALOAD (3 gas)
Impact
매번 storage 변수를 load하여 가스가 낭비된다.
Mitigation
storage array의 length를 루프에 이용하는 경우 로컬 변수에 캐싱하여 이용한다.
tags: bughunting, nouns dao, smart contract, solidity, gas optimization, gas, solidity storage, solidity memory, severity gas