- The test case below aims to show that due to the incorrect validation in Stable2.sol, the
calcLpTokenSupply
exhibits weird behaviour.
First we add this test to Stable2.t.sol and run using forge test --mt test_sameDecimals2 -vvv
Based on our expectations that a returned decimal data of 0 should be set to 18, and as such, subsequent calculation calculations should continue normally, we would expect this function to pass, but it doesn't.
function test_calcLpTokenSupply_sameDecimals2() public {
_data = abi.encode(18, 0);
uint256[] memory reserves = new uint256[](2);
reserves[0] = STATE_A_B0;
reserves[1] = STATE_A_B1;
assertEq(_function.calcLpTokenSupply(reserves, _data), STATE_A_LP);
}
The test however fails with the error message below.
[FAIL. Reason: panic: arithmetic underflow or overflow (0x11)] test_calcLpTokenSupply_sameDecimals2() (gas: 61838)
Traces:
[61838] Stable2Test::test_calcLpTokenSupply_sameDecimals2()
├─ [4095] Stable2::calcLpTokenSupply([10000000000000000000 [1e19], 10000000000000000000 [1e19]], 0x00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000) [staticcall]
│ └─ ← [Revert] panic: arithmetic underflow or overflow (0x11)
└─ ← [Revert] panic: arithmetic underflow or overflow (0x11)
Suite result: FAILED. 0 passed; 1 failed; 0 skipped; finished in 1.01ms (98.33µs CPU time)
Ran 1 test suite in 151.81ms (1.01ms CPU time): 0 tests passed, 1 failed, 0 skipped (1 total tests)
Failing tests:
Encountered 1 failing test in test/functions/Stable2.t.sol:Stable2Test
[FAIL. Reason: panic: arithmetic underflow or overflow (0x11)] test_calcLpTokenSupply_sameDecimals2() (gas: 61838)
Encountered a total of 1 failing tests, 0 tests succeeded
Now, if we fix the code, by pasting the recommendation below
- if (decimal0 == 0) {
+ if (decimal1 == 0) {
decimal1 = 18;
}
and running the test again, it succeeds.
[PASS] test_calcLpTokenSupply_sameDecimals2() (gas: 67974)
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 1.41ms (125.25µs CPU time)
Ran 1 test suite in 155.68ms (1.41ms CPU time): 1 tests passed, 0 failed, 0 skipped (1 total tests)
For control, by running the default test. It also passes, showing that the recommendation didn't break anything.
[PASS] test_calcLpTokenSupply_sameDecimals() (gas: 87926)