code4rena-2022-08-nounsdao-g09
[G‑09] Add unchecked {} for subtractions where the operands cannot underflow because of a previous require() or if-statement
Summary
언더플로우/오버플로우가 나지 않을 상황이라면 unchecked 를 넣어 가스비를 절약하자고 제안했다.
Keyword
gas optimization, underflow, unchecked
Vulnerability
File: contracts/base/ERC721Checkpointable.sol
/// @audit require() on line 278
278: require(b <= a, errorMessage);
279: return a - b;언더플로우/오버플로우가 나지 않을 상황이라면 unchecked 를 넣어 safeMath 체크를 중복으로 하지 않도록 해도 된다. 이러면 가스비가 절약된다.
위 상황의 경우 require(b <= a, errorMessage); 를 먼저 확인했으므로 return a - b; 에서 언더플로우가 일어나지 않는다.
따라서 언더플로우를 다시 체크하지 않아도 괜찮다. 즉, unchecked 해도 괜찮다.
Impact
언더플로우를 중복으로 체크하여 가스비를 낭비한다.
Mitigation
require(a <= b); x = b - a ⇒ require(a <= b); unchecked { x = b - a }
unchecked 를 넣어 중복 체크를 방지한다.
tags: bughunting, nouns dao, smart contract, solidity, gas optimization, gas, integer overflow underflow, solidity unchecked, solidity safemath, severity gas