Skip to content

Commit 30be88d

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 3b19ac6 commit 30be88d

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;
@@ -588,8 +589,9 @@ contract AllocationManager is
588589
return int128(uint128(newMagnitude)) - int128(uint128(currentMagnitude));
589590
}
590591

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

595597
/**

src/test/unit/AllocationManagerUnit.t.sol

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

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

0 commit comments

Comments
 (0)