Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse arithmetic results can save gas #66

Open
code423n4 opened this issue Dec 8, 2021 · 3 comments
Open

Reuse arithmetic results can save gas #66

code423n4 opened this issue Dec 8, 2021 · 3 comments
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

WatchPug

Vulnerability details

https://github.com/maple-labs/debt-locker/blob/81f55907db7b23d27e839b9f9f73282184ed4744/contracts/DebtLocker.sol#L205-L215

function _handleClaimOfRepossessed() internal returns (uint256[7] memory details_) {
    ...
    details_[0] = recoveredFunds + fundsCaptured;
    details_[1] = recoveredFunds > principalToCover ? recoveredFunds - principalToCover : 0;
    details_[2] = fundsCaptured;
    details_[5] = recoveredFunds > principalToCover ? principalToCover : recoveredFunds;
    details_[6] = principalToCover > recoveredFunds ? principalToCover - recoveredFunds : 0;

    _fundsToCapture = uint256(0);
    _repossessed    = false;

    require(ERC20Helper.transfer(fundsAsset, _pool, recoveredFunds + fundsCaptured), "DL:HCOR:TRANSFER");
}

recoveredFunds + fundsCaptured at L215 is calculated before at L205, since it's a checked arithmetic operation with two memory variables, resue the result instead of doing the arithmetic operation again can save gas.

Recommendation

Change to:

require(ERC20Helper.transfer(fundsAsset, _pool, details_[0]), "DL:HCOR:TRANSFER");

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Dec 8, 2021
code423n4 added a commit that referenced this issue Dec 8, 2021
@lucas-manuel
Copy link
Collaborator

Thanks will add

@lucas-manuel lucas-manuel added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Dec 8, 2021
@deluca-mike
Copy link
Collaborator

Actually, the MLOAD for details_[0] is more expensive than the DUPs and ADD of another recoveredFunds + fundsCaptured. Instead, we will just cache the result on the stack before L205, and use it twice.

@pauliax
Copy link
Collaborator

pauliax commented Dec 15, 2021

As per the sponsor's comment, the mitigation suggested by the warden is not cheaper but anyway it helped to find an optimization so I am leaving this issue as valid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

4 participants