From a03ef3454966d8a83c3ecc8e0fbf76a790ef6189 Mon Sep 17 00:00:00 2001 From: baroooo Date: Mon, 25 Nov 2024 10:25:59 +0100 Subject: [PATCH 1/2] feat: use MIN_INT in safe add --- contracts/libraries/TradingLimits.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/libraries/TradingLimits.sol b/contracts/libraries/TradingLimits.sol index dfde452..773358f 100644 --- a/contracts/libraries/TradingLimits.sol +++ b/contracts/libraries/TradingLimits.sol @@ -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); } } From 0a88b8eea212e8ff277d747118aea0ec120c0ec1 Mon Sep 17 00:00:00 2001 From: baroooo Date: Mon, 25 Nov 2024 10:45:11 +0100 Subject: [PATCH 2/2] test: safeadd with underflow --- test/unit/libraries/TradingLimits.t.sol | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/test/unit/libraries/TradingLimits.t.sol b/test/unit/libraries/TradingLimits.t.sol index fc99aa2..abb3066 100644 --- a/test/unit/libraries/TradingLimits.t.sol +++ b/test/unit/libraries/TradingLimits.t.sol @@ -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); } }