Summary
this library is attempting to revert with custom errors signatures, but fails the test.
Signature code is wrong as well.
mstore(0x40, 0xbac65e5b) // MathMasters__MulWadFailed()
. code 0xbac65e5b is not the correct signature for MathMasters_MulWadFailed()
using cast we geth the 4byte signature code 0xa56044f7
cast sig "MathMasters__MulWadFailed()"
0xa56044f7
Original Code | ::
function mulWad(uint256 x, uint256 y) internal pure returns (uint256 z) {
assembly {
if mul(y, gt(x, div(not(0), y))) {
mstore(0x40, 0xbac65e5b)
revert(0x1c, 0x04)
}
z := div(mul(x, y), WAD)
}
}
function test_MulWad_customError_() public {
vm.expectRevert(MathMasters.MathMasters__MulWadFailed.selector);
MathMasters.mulWad(type(uint256).max + 1, 0);
}
......
[FAIL. Reason: Error != expected error: NH{q◄ != 0xa56044f7] test_MulWad_customError_() (gas: 3131)
Traces:
[3131] MathMastersTest::test_MulWad_customError_()
├─ [0] VM::expectRevert(0xa56044f7)
│ └─ ← ()
└─ ← "Arithmetic over/underflow"
Test result: FAILED. 1 passed; 1 failed; finished in 731.50µs
Vulnerability Details
unneccesary code can add to gas cost. It also make the code less readable and prone to more errors.
Impact
unnessary gas cost
Tools Used
Forge
Recommendations
Because the contract is using ^0.8.3, underflow/ overflow protection is built into solidity. In both mulWad and mulWadUp remove Remove Custom Errors assembly line code and revert.
if mul(y, gt(x, div(not(0), y))) {
revert(0, 0)
}
[PASS] test_MulWad_customError_() (gas: 3131)
Traces:
[3131] MathMastersTest::test_MulWad_customError_()
├─ [0] VM::expectRevert()
│ └─ ← ()
└─ ← "Arithmetic over/underflow"