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

Uncheck Arithmetic where overflow/underflow impossible

Summary

Use unchecked blocks in code parts where overflow or underflow is not possible to save on gas

Vulnerability Details

Impact

Gas savings: Solidity compiler from 0.8.0 upwards does default internal overflow and underflow checks for arithmetic.
This adds more computation to functions increasing gas costs. However its possible to use unchecked blocks in order to avoid these compiler operation checks in order to save on gas

Tools Used

Manual Analysis

Recommendations

  1. DSCEngine.sol line 118 the ++i in for loop can be placed in an unchecked
    for (uint256 i = 0; i < tokenAddresses.length;) {
    s_priceFeeds[tokenAddresses[i]] = priceFeedAddresses[i];
    s_collateralTokens.push(tokenAddresses[i]);
    unchecked { ++i}
    }

  2. DSCEngine.sol line 155 can be rewritten as
    unchecked {s_collateralDeposited[msg.sender][tokenCollateralAddress] += amountCollateral;}
    Even if a flashloand where used there would be no such liquidity to get to large number 1157920892373161954235709850086879078532699846656405640394575 needed to overflow uint256

  3. DSCEnginel.sol line 198 can be rewritten as
    unchecked {s_DSCMinted[msg.sender] += amountDscToMint;}
    Even if a flashloan where used there would be no such liquidity to get to large number 1157920892373161954235709850086879078532699846656405640394575 needed to overflow uint256

  4. DSCEnginel.sol line 252 can be rewritten as
    unchecked {uint256 totalCollateralToRedeem = tokenAmountFromDebtCovered + bonusCollateral;}
    Even if a flashloan where used there would be no such liquidity to get to large number 1157920892373161954235709850086879078532699846656405640394575 needed to overflow uint256

  5. DSCEngine.sol line 353 the ++i in for loop can be placed in an unchecked
    for (uint256 i = 0; i < s_collateralTokens.length; i++) {
    address token = s_collateralTokens[i];
    uint256 amount = s_collateralDeposited[user][token];
    totalCollateralValueInUsd += getUsdValue(token, amount);
    unchecked {
    ++i;
    totalCollateralValueInUsd += getUsdValue(token, amount);
    }
    }
    Again adding 1 for ++i cant reach max size uint256 in addition to the adding USD value

Support

FAQs

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