20,000 USDC
View results
Submission Details
Severity: gas
Valid

Unwanted gas expenditure by recomputing already computed value

Summary

Unwanted gas is spent on recomputing a value that has already been computed.

Vulnerability Details

https://github.com/Cyfrin/2023-07-beedle/blob/658e046bda8b010a5b82d2d85e824f3823602d27/src/Lender.sol#L355-L432

function giveLoan(
uint256[] calldata loanIds,
bytes32[] calldata poolIds
) external {
for (uint256 i = 0; i < loanIds.length; i++) {
..... more code
// totalDebt is computed here
uint256 totalDebt = loan.debt + lenderInterest + protocolInterest;
...... more code
emit Repaid(
loan.borrower,
loan.lender,
loanId,
// totalDebt is recomputed here
loan.debt + lenderInterest + protocolInterest,
loan.collateral,
loan.interestRate,
loan.startTimestamp
);
..... more code
}

https://github.com/Cyfrin/2023-07-beedle/blob/658e046bda8b010a5b82d2d85e824f3823602d27/src/Lender.sol#L465-L515

function buyLoan(uint256 loanId, bytes32 poolId) public {
..... more code
// totalDebt is computed here
uint256 totalDebt = loan.debt + lenderInterest + protocolInterest;
...... more code
emit Repaid(
loan.borrower,
loan.lender,
loanId,
// totalDebt is recomputed here
loan.debt + lenderInterest + protocolInterest,
loan.collateral,
loan.interestRate,
loan.startTimestamp
);
..... more code
}

Impact

Unwanted gas expenditure

Tools Used

Forge gas reporter

Recommendations

Avoid recomputation and use the already computed value

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.