Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/use correct min for int48 #556

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/libraries/TradingLimits.sol
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ library TradingLimits {
*/
function safeINT48Add(int48 a, int48 b) internal pure returns (int48) {
int256 c = int256(a) + int256(b);
require(c >= -1 * MAX_INT48 && c <= MAX_INT48, "int48 addition overflow");
require(c >= MIN_INT48 && c <= MAX_INT48, "int48 addition overflow");
return int48(c);
}
}
17 changes: 15 additions & 2 deletions test/unit/libraries/TradingLimits.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,23 @@ contract TradingLimitsTest is Test {

function test_update_withOverflowOnAdd_reverts() public {
ITradingLimits.Config memory config = configLG(int48(uint48(2 ** 47)));
int256 maxFlow = int256(uint256(type(uint48).max / 2));
int256 maxFlow = int256(type(int48).max);

state = harness.update(state, config, (maxFlow - 1000) * 1e18, 18);
state = harness.update(state, config, 1000 * 1e18, 18);

vm.expectRevert(bytes("int48 addition overflow"));
state = harness.update(state, config, 1 * 1e18, 18);
}

function test_update_withUnderflowOnAdd_reverts() public {
ITradingLimits.Config memory config = configLG(int48(uint48(2 ** 47)));
int256 minFlow = int256(type(int48).min);

state = harness.update(state, config, (minFlow + 1000) * 1e18, 18);
state = harness.update(state, config, -1000 * 1e18, 18);

vm.expectRevert(bytes("int48 addition overflow"));
state = harness.update(state, config, 1002 * 10e18, 18);
state = harness.update(state, config, -1 * 1e18, 18);
}
}
Loading