Skip to content

Commit a6f379c

Browse files
authored
fix: Add safecast on MathUtils::signedSub function (#1071)
1 parent 8c2c906 commit a6f379c

File tree

7 files changed

+41
-29
lines changed

7 files changed

+41
-29
lines changed

snapshots/Hub.Operations.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"remove: partial": "81640",
1212
"reportDeficit": "115225",
1313
"restore: full": "80471",
14-
"restore: full - with transfer": "173377",
14+
"restore: full - with transfer": "173521",
1515
"restore: partial": "89137",
1616
"restore: partial - with transfer": "147400",
1717
"transferShares": "71192"

snapshots/NativeTokenGateway.Operations.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"borrowNative": "229316",
3-
"repayNative": "168024",
2+
"borrowNative": "229604",
3+
"repayNative": "168312",
44
"supplyAsCollateralNative": "160373",
55
"supplyNative": "136476",
66
"withdrawNative: full": "125620",

snapshots/SignatureGateway.Operations.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"borrowWithSig": "215605",
3-
"repayWithSig": "188872",
2+
"borrowWithSig": "215893",
3+
"repayWithSig": "189160",
44
"setSelfAsUserPositionManagerWithSig": "75402",
55
"setUsingAsCollateralWithSig": "85053",
66
"supplyWithSig": "153205",

snapshots/Spoke.Operations.ZeroRiskPremium.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
22
"borrow: first": "191325",
33
"borrow: second action, same reserve": "171297",
4-
"liquidationCall (receiveShares): full": "300103",
5-
"liquidationCall (receiveShares): partial": "299821",
6-
"liquidationCall: full": "310468",
7-
"liquidationCall: partial": "310186",
8-
"permitReserve + repay (multicall)": "166029",
4+
"liquidationCall (receiveShares): full": "300391",
5+
"liquidationCall (receiveShares): partial": "300109",
6+
"liquidationCall: full": "310756",
7+
"liquidationCall: partial": "310474",
8+
"permitReserve + repay (multicall)": "166317",
99
"permitReserve + supply (multicall)": "146862",
1010
"permitReserve + supply + enable collateral (multicall)": "160573",
11-
"repay: full": "126094",
12-
"repay: partial": "130983",
11+
"repay: full": "126382",
12+
"repay: partial": "131271",
1313
"setUserPositionManagerWithSig: disable": "44846",
1414
"setUserPositionManagerWithSig: enable": "68875",
1515
"supply + enable collateral (multicall)": "140624",

snapshots/Spoke.Operations.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"borrow: first": "261721",
3-
"borrow: second action, same reserve": "204693",
4-
"liquidationCall (receiveShares): full": "333666",
5-
"liquidationCall (receiveShares): partial": "333384",
6-
"liquidationCall: full": "344031",
7-
"liquidationCall: partial": "343749",
8-
"permitReserve + repay (multicall)": "163273",
2+
"borrow: first": "262009",
3+
"borrow: second action, same reserve": "204981",
4+
"liquidationCall (receiveShares): full": "334242",
5+
"liquidationCall (receiveShares): partial": "333960",
6+
"liquidationCall: full": "344607",
7+
"liquidationCall: partial": "344325",
8+
"permitReserve + repay (multicall)": "163504",
99
"permitReserve + supply (multicall)": "146862",
1010
"permitReserve + supply + enable collateral (multicall)": "160573",
11-
"repay: full": "120256",
12-
"repay: partial": "139545",
11+
"repay: full": "120544",
12+
"repay: partial": "139833",
1313
"setUserPositionManagerWithSig: disable": "44846",
1414
"setUserPositionManagerWithSig: enable": "68875",
1515
"supply + enable collateral (multicall)": "140624",
@@ -18,16 +18,16 @@
1818
"supply: second action, same reserve": "106579",
1919
"updateUserDynamicConfig: 1 collateral": "73694",
2020
"updateUserDynamicConfig: 2 collaterals": "88551",
21-
"updateUserRiskPremium: 1 borrow": "151080",
22-
"updateUserRiskPremium: 2 borrows": "204276",
21+
"updateUserRiskPremium: 1 borrow": "151368",
22+
"updateUserRiskPremium: 2 borrows": "204852",
2323
"usingAsCollateral: 0 borrows, enable": "58915",
24-
"usingAsCollateral: 1 borrow, disable": "161348",
24+
"usingAsCollateral: 1 borrow, disable": "161636",
2525
"usingAsCollateral: 1 borrow, enable": "41803",
26-
"usingAsCollateral: 2 borrows, disable": "233712",
26+
"usingAsCollateral: 2 borrows, disable": "234288",
2727
"usingAsCollateral: 2 borrows, enable": "41815",
2828
"withdraw: 0 borrows, full": "128910",
2929
"withdraw: 0 borrows, partial": "133473",
30-
"withdraw: 1 borrow, partial": "214810",
31-
"withdraw: 2 borrows, partial": "259272",
30+
"withdraw: 1 borrow, partial": "215098",
31+
"withdraw: 2 borrows, partial": "259848",
3232
"withdraw: non collateral": "106544"
3333
}

src/libraries/math/MathUtils.sol

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
// Copyright (c) 2025 Aave Labs
33
pragma solidity ^0.8.20;
44

5+
import {SafeCast} from 'src/dependencies/openzeppelin/SafeCast.sol';
6+
57
/// @title MathUtils library
68
/// @author Aave Labs
79
library MathUtils {
10+
using SafeCast for uint256;
11+
812
uint256 internal constant RAY = 1e27;
913
/// @dev Ignoring leap years
1014
uint256 internal constant SECONDS_PER_YEAR = 365 days;
@@ -50,9 +54,8 @@ library MathUtils {
5054
}
5155

5256
/// @notice Returns the difference of two unsigned integers as a signed integer.
53-
/// @dev Does not ensure the `a` and `b` values are within the range of a signed integer.
5457
function signedSub(uint256 a, uint256 b) internal pure returns (int256) {
55-
return int256(a) - int256(b);
58+
return a.toInt256() - b.toInt256();
5659
}
5760

5861
/// @notice Returns the difference of two unsigned integers.

tests/unit/MathUtils.t.sol

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,15 @@ contract MathUtilsTest is Base {
127127
assertTrue(result <= INT256_MAX);
128128
}
129129

130+
function test_signedSub_revertsWith_SafeCastOverflowedUintToInt(uint256 a) public {
131+
a = bound(a, uint256(INT256_MAX) + 1, UINT256_MAX);
132+
vm.expectRevert(abi.encodeWithSelector(SafeCast.SafeCastOverflowedUintToInt.selector, a));
133+
MathUtils.signedSub(a, 0);
134+
135+
vm.expectRevert(abi.encodeWithSelector(SafeCast.SafeCastOverflowedUintToInt.selector, a));
136+
MathUtils.signedSub(0, a);
137+
}
138+
130139
function test_uncheckedSub(uint256 a, uint256 b) public pure {
131140
uint256 result = a >= b ? a - b : UINT256_MAX - b + a + 1;
132141
assertEq(MathUtils.uncheckedSub(a, b), result);

0 commit comments

Comments
 (0)