Beginner FriendlyFoundry
100 EXP
View results
Submission Details
Severity: high
Valid

The rounding up in `mulWadUp` should be done after the multiplication

Summary

The rounding up for the multiplication of two numbers is done before the multiplication, which will produce invalid output.

Vulnerability Details

The function is used to calculate the multiplication of two numbers and round up the result. If the result is not rounded up correctly, it means it will produce invalid result, therefore affecting thr protocols and contracts expecting a correct result.

Impact

The impact is high, as invalid outputs may affect the protocols and contracts. One example may be that the protocol is expecting a certain amount of tokens to be minted, but the result is not rounded up correctly, therefore the amount of tokens minted is not correct and it would hurt the protocol in a long run.

Tools Used

Foundry fuzz tests

Recommendations

The rounding up should be done after the the multiplication.

function mulWadUp(uint256 x, uint256 y) internal pure returns (uint256 z) {
/// @solidity memory-safe-assembly
assembly {
// Equivalent to `require(y == 0 || x <= type(uint256).max / y)`.
if mul(y, gt(x, or(div(not(0), y), x))) {
mstore(0x40, 0xbac65e5b) // `MathMasters__MulWadFailed()`.
revert(0x1c, 0x04)
}
- if iszero(sub(div(add(z, x), y), 1)) { x := add(x, 1) }
z := add(iszero(iszero(mod(mul(x, y), WAD))), div(mul(x, y), WAD))
+ if iszero(sub(div(add(z, x), y), 1)) { x := add(x, 1) }
}
}
Updates

Lead Judging Commences

inallhonesty Lead Judge
over 1 year ago
inallhonesty Lead Judge over 1 year ago
Submission Judgement Published
Validated
Assigned finding tags:

`mulWadUp` has an unnecessary line that makes the result wrong for some inputs

Support

FAQs

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