Skip to content

Commit 9e1da61

Browse files
8sunyuanwadealexc
authored andcommitted
fix: overflow bug for pendingDiff input (#1027)
* fix: overflow bug for pendingDiff input * test: add check to regression test --------- Co-authored-by: wadealexc <pragma-services@proton.me>
1 parent 7d88429 commit 9e1da61

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/contracts/core/AllocationManager.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ contract AllocationManager is
2121
{
2222
using DoubleEndedQueue for DoubleEndedQueue.Bytes32Deque;
2323
using EnumerableSet for *;
24+
using SafeCast for *;
2425

2526
using Snapshots for Snapshots.DefaultWadHistory;
2627
using OperatorSetLib for OperatorSet;
@@ -586,8 +587,9 @@ contract AllocationManager is
586587
return int128(uint128(newMagnitude)) - int128(uint128(currentMagnitude));
587588
}
588589

590+
/// @dev Use safe casting when downcasting to uint64
589591
function _addInt128(uint64 a, int128 b) internal pure returns (uint64) {
590-
return uint64(uint128(int128(uint128(a)) + b));
592+
return uint256(int256(int128(uint128(a)) + b)).toUint64();
591593
}
592594

593595
/**

src/test/unit/AllocationManagerUnit.t.sol

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,58 @@ contract AllocationManagerUnitTests_ModifyAllocations is AllocationManagerUnitTe
19931993
allocationManager.modifyAllocations(defaultOperator, allocateParams);
19941994
}
19951995

1996+
function test_revert_safeCastOverflow() public {
1997+
// setup additional operatorSets for tests
1998+
OperatorSet memory opSet1 = OperatorSet(defaultAVS, 1);
1999+
_createOperatorSet(opSet1, defaultStrategies);
2000+
_registerOperator(defaultOperator);
2001+
_setAllocationDelay(defaultOperator, DEFAULT_OPERATOR_ALLOCATION_DELAY);
2002+
_registerForOperatorSet(defaultOperator, opSet1);
2003+
2004+
OperatorSet memory opSet2 = OperatorSet(defaultAVS, 2);
2005+
_createOperatorSet(opSet2, defaultStrategies);
2006+
_registerOperator(defaultOperator);
2007+
_setAllocationDelay(defaultOperator, DEFAULT_OPERATOR_ALLOCATION_DELAY);
2008+
_registerForOperatorSet(defaultOperator, opSet2);
2009+
2010+
// 1. Allocate all available magnitude for the strategy (WAD)
2011+
AllocateParams[] memory allocateParams = _randAllocateParams_DefaultOpSet();
2012+
allocateParams[0].newMagnitudes[0] = WAD;
2013+
cheats.prank(defaultOperator);
2014+
allocationManager.modifyAllocations(defaultOperator, allocateParams);
2015+
assertEq(
2016+
allocationManager.getAllocatableMagnitude(defaultOperator, strategyMock),
2017+
0,
2018+
"Allocatable magnitude should be 0"
2019+
);
2020+
assertEq(
2021+
allocationManager.getEncumberedMagnitude(defaultOperator, strategyMock),
2022+
WAD,
2023+
"Encumbered magnitude should be WAD"
2024+
);
2025+
2026+
// 2. allocate to another operatorSet for the same strategy to reset encumberedMagnitude back to 0
2027+
allocateParams[0].operatorSet = opSet1;
2028+
allocateParams[0].newMagnitudes[0] = type(uint64).max - WAD + 1;
2029+
cheats.prank(defaultOperator);
2030+
cheats.expectRevert("SafeCast: value doesn't fit in 64 bits");
2031+
allocationManager.modifyAllocations(defaultOperator, allocateParams);
2032+
2033+
// 3. after resetting encumberedMagnitude, attempt to allocate to opSet2 with WAD
2034+
allocateParams[0].operatorSet = opSet2;
2035+
allocateParams[0].newMagnitudes[0] = WAD;
2036+
cheats.prank(defaultOperator);
2037+
cheats.expectRevert(InsufficientMagnitude.selector);
2038+
allocationManager.modifyAllocations(defaultOperator, allocateParams);
2039+
2040+
// 4. after resetting encumberedMagnitude, attempt to allocate to opSet2 with 1
2041+
allocateParams[0].operatorSet = opSet2;
2042+
allocateParams[0].newMagnitudes[0] = 1;
2043+
cheats.prank(defaultOperator);
2044+
cheats.expectRevert(InsufficientMagnitude.selector);
2045+
allocationManager.modifyAllocations(defaultOperator, allocateParams);
2046+
}
2047+
19962048
/**
19972049
* @notice Tests edge cases around allocation delay:
19982050
* 1. Set allocation delay to a value greater than ALLOCATION_CONFIGURATION_DELAY

0 commit comments

Comments
 (0)