DeFiFoundry
50,000 USDC
View results
Submission Details
Severity: low
Invalid

Division by Zero in _mint

Summary

The _mint function contains a division by zero vulnerability that can result in unfair share distributions.

Vulnerability Details

Location

_mint function

Issue

The function calculates _shares using the following logic:

if (totalShares == 0) {
_shares = depositInfo[depositId].amount * 1e8;
} else {
uint256 totalAmountBefore;
if (positionIsClosed == false && _isLongOneLeverage(beenLong)) {
totalAmountBefore = IERC20(indexToken).balanceOf(address(this)) - amount;
} else {
totalAmountBefore = _totalAmount(prices) - amount;
}
if (totalAmountBefore == 0) totalAmountBefore = 1;
_shares = amount * totalShares / totalAmountBefore;
}

The problem occurs when totalAmountBefore == 0, where it is forcefully set to 1. This prevents a division-by-zero error but leads to incorrect share calculations.

Impact

  • Unfair Share Distribution: The use of totalAmountBefore = 1 can cause inflated or incorrect share allocation, leading to some depositors getting an unfair advantage.

  • Potential Exploitation: A user could deposit when totalAmountBefore == 0 to receive disproportionate shares, diluting existing holders.

  • Incorrect Accounting: The contract's share issuance mechanism becomes unreliable, possibly affecting future operations.

Tools Used

  • Manual code review

  • Static analysis

Recommendations

Instead of setting totalAmountBefore = 1, implement a proper fix:

  1. Ensure Proper Initialization

    • Validate totalAmountBefore before performing the division.

    • Check if totalAmountBefore == 0 and handle it gracefully instead of assigning 1.

  2. Revert Instead of Hardcoding a Value

    require(totalAmountBefore > 0, "Invalid total amount before minting shares");

    This prevents transactions from proceeding under invalid conditions.

Updates

Lead Judging Commences

n0kto Lead Judge 6 months ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

invalid_totalAmountBefore_is_1_incorrect_calculation_supposition

No proof when this can happen: Most of the time totalAmountBefore equals 0 (balance minus amount sent), it means totalShares equals 0. If it could happen with very specific conditions, report with that tag didn't add the needed details to be validated.

Support

FAQs

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