MorpheusAI

MorpheusAI
Foundry
22,500 USDC
View results
Submission Details
Severity: medium
Invalid

_calculateFullPeriodReward will revert due to underflow

Vulnerability Details

In LinearDistributionIntervalDecrease the _calculateFullPeriodReward carries out calculation that can result in underflow due to subtracting with a larger value that will gives negative number and as a result will underflow.
Lets consider the value for variable ip

uint256 ip_ = ((endTime_ - payoutStart_ - intervalsPassedBefore_ * interval_) / interval_);
uint256 ip_ = ((20 - 10 - 15 * 5) / 5);

In the above expression, the term 15 * 5 evaluates to 75, and subtracting this from 20 - 10 results in -65. Since negative value cannot be assign to uint256, the compiler throws an error.

Running a simple test clearly gives compiler error :

forge test --mt test_balanceAddress -vvv
[⠔] Compiling...
[⠒] Compiling 1 files with 0.8.20
[⠆] Solc 0.8.20 finished in 1.50s
Error:
Compiler run failed:
Error (9574): Type int_const -13 is not implicitly convertible to expected type uint256. Cannot implicitly convert signed literal to unsigned type.
--> test/price.t.sol:64:9:
|
64 | uint256 ip_ = ((20 - 10 - 15 * 5) / 5);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Impact

In Solidity, perform arithmetic operations involving negative numbers, the result can be a negative number, which cannot be assigned to a uint256 because unsigned integers cannot represent negative values.
This will cause the method to revert due to underflow.

Recommendations

To fix the problem of underflow as a result of negative number is to ensure that the expression does not evaluate to a negative number.
The recommendation to avoid the error is to rearrange the terms so that the subtraction happens on result of multiplied value that will be greater and would prevent the result from being negative.

- uint256 ip_ = ((endTime_ - payoutStart_ - intervalsPassedBefore_ * interval_) / interval_);
+ uint256 ip_ = (((intervalsPassedBefore_ * interval_) – (endTime_ - payoutStart_)) / interval_);
Updates

Lead Judging Commences

inallhonesty Lead Judge
over 1 year ago
inallhonesty Lead Judge over 1 year ago
Submission Judgement Published
Invalidated
Reason: Incorrect statement

Support

FAQs

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