DeFiHardhat
12,000 USDC
View results
Submission Details
Severity: low
Invalid

Incorrect `LibMath.mulDivOrMax` function logic description

Summary

There is a discrepancy between the LibMath.mulDivOrMax function logic and the corresponding comment.

Vulnerability Details

The LibMath.mulDivOrMax returns type(uint256).max in case of potential overflow. But the corresponding comment mentions another logic.

/**
>> * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
* with further edits by Uniswap Labs also under MIT license.
*/
function mulDivOrMax(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2^256 + prod0.
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
// Solidity will revert if denominator == 0, unlike the div opcode on its own.
// The surrounding unchecked block does not change this fact.
// See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
return prod0 / denominator;
}
// Make sure the result is less than 2^256. Also prevents denominator == 0.
>> if (denominator <= prod1) return type(uint256).max;

Impact

Discrepancy between code and comments can lead to difficulties and mistakes during protocol maintenance.

Tools used

Manual Review

Recommendations

Consider changing the comment on the correct one.

- * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
+ * @notice Calculates floor(x * y / denominator) with full precision. Returns type(uint256).max if result overflows a uint256. Throws if denominator == 0
Updates

Lead Judging Commences

giovannidisiena Lead Judge about 1 year ago
Submission Judgement Published
Invalidated
Reason: Non-acceptable severity
Assigned finding tags:

Informational/Invalid

Support

FAQs

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