Skip to content

Commit

Permalink
Fix/use correct min for int48 (#556)
Browse files Browse the repository at this point in the history
### Description

Fixes Sherlock issue 46 by using `type(int48).min`

### Other changes

updates test for withOverflowOnAdd to test the correct limit

### Tested

unit tests

### Related issues

- Fixes
[#598](mento-protocol/mento-general#598)
  • Loading branch information
baroooo authored Nov 27, 2024
1 parent 9653183 commit 8236d65
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion contracts/libraries/TradingLimits.sol
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,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 @@ -316,10 +316,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);
}
}

0 comments on commit 8236d65

Please sign in to comment.