code-423n4 / 2021-09-wildcredit-findings

0 stars 0 forks source link

Use unchecked{} primitive to save gas where possible #84

Open code423n4 opened 3 years ago

code423n4 commented 3 years ago

Handle

0xRajeev

Vulnerability details

Impact

Given the use of Solidity compiler >= 0.8.0, there are default arithmetic checks for mathematical operations which consume additional gas for such checks internally. In expressions where we are absolutely sure of no overflows/underflows, one can use the unchecked{} primitive to wrap such expressions to avoid checks and save gas.

For example, given the check on L419, we can use the unchecked{} directive on L420 because repayAmount is guaranteed to be <= _amount due to the prior check.

Proof of Concept

https://github.com/code-423n4/2021-09-wildcredit/blob/c48235289a25b2134bb16530185483e8c85507f8/contracts/LendingPair.sol#L419-L420

https://github.com/code-423n4/2021-09-wildcredit/blob/c48235289a25b2134bb16530185483e8c85507f8/contracts/LendingPair.sol#L434-L435

Tools Used

Manual Analysis

Recommended Mitigation Steps

Use unchecked{} primitive to wrap arithmetic expressions where we are absolutely sure of no overflows/underflows. This avoids built-in checks and saves gas.