When a function call fails due to overflow, it does not revert with the intended error. Instead, it reverts without providing a reason.
The information regarding the reverted error is stored at the memory location of the free memory pointer (0x40
), but the functions revert with the data stored at the memory location 0x1c
.
It's important to note that there is no need to override the free memory pointer in this case, and it is not being updated in a secure manner. This overriding will result in very expensive memory expansion, and If the function wouldn't revert, there would be a possibility of encountering "Out of Gas" error.
When an overflow occurs, the function reverts without providing an intended error.
We can check whether the function call will revert with the intended error.
Add the following test in MathMasters.t.sol
:
Run a test with forge test --mt test_MulWadShouldRevertWithIntendedError
.
We should see an output similar to this in the terminal:
This test has failed because the function call did revert, but not with an error that was expected in the test.
Manual review
Foundry
The reason for reverting should be stored in memory in the same location that revert
uses to locate the data it's supposed to revert with.
revert
returns four bytes (0x04
) starting from memory location 0x1c
. A four-byte error, when stored in memory, will be padded to 32 bytes. According to this, we can easily calculate the location where the error should be stored in memory:
(0x1c + 0x04) - 0x20 = 0x0
, where 0x20
is the hexadecimal representation of 32.
The error should be stored at the memory location 0x00
, which is known as scratch space (Layout in Memory).
Recommended changes to the MathMasters.sol::mulWad()
function:
Recommended changes to the MathMasters.sol::mulWadUp()
function:
If we attempt to execute the same test that was used in the PoC, we can see that it now passes as the function call reverted with the expected error.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.