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

Increments can be unchecked in for-loops

Summary

In Solidity 0.8+, there’s a default overflow check on unsigned integers, which we can skip when iterating over loops and therefore save gas. The risk of overflow is non-existent for uint256 here.

Vulnerability Details

Impact

Pointless gas waste.

Tools Used

Manual Review

Recommendations

Consider wrapping with an unchecked block (around 25 gas saved per instance).
Example in Lender.sol:438

/// @notice start a refinance auction
/// can only be called by the lender
/// @param loanIds the ids of the loans to refinance
function startAuction(uint256[] calldata loanIds) public {
- for (uint256 i = 0; i < loanIds.length; i++) {
+ for (uint256 i = 0; i < loanIds.length;) {
uint256 loanId = loanIds[i];
// get the loan info
Loan memory loan = loans[loanId];
// validate the loan
if (msg.sender != loan.lender) revert Unauthorized();
if (loan.auctionStartTimestamp != type(uint256).max)
revert AuctionStarted();
// set the auction start timestamp
loans[loanId].auctionStartTimestamp = block.timestamp;
emit AuctionStart(
loan.borrower,
loan.lender,
loanId,
loan.debt,
loan.collateral,
block.timestamp,
loan.auctionLength
);
}
+ unchecked { ++i};
}

Support

FAQs

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