Skip to content

Commit

Permalink
fix: move collateralHints check
Browse files Browse the repository at this point in the history
  • Loading branch information
lekhovitsky committed Oct 31, 2023
1 parent 5b31f96 commit 5636dbe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
9 changes: 8 additions & 1 deletion contracts/credit/CreditManagerV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,14 @@ contract CreditManagerV3 is ICreditManagerV3, SanityCheckTrait, ReentrancyGuardT
revert CustomHealthFactorTooLowException(); // U:[CM-17]
}

unchecked {
uint256 len = collateralHints.length;
for (uint256 i; i < len; ++i) {
uint256 mask = collateralHints[i];
if (mask == 0 || mask & mask - 1 != 0) revert InvalidCollateralHintException(); // U:[CM-17]
}
}

CollateralDebtData memory cdd = _calcDebtAndCollateral({
creditAccount: creditAccount,
minHealthFactor: minHealthFactor,
Expand Down Expand Up @@ -778,7 +786,6 @@ contract CreditManagerV3 is ICreditManagerV3, SanityCheckTrait, ReentrancyGuardT
uint256 tokenMask;
if (hintsIdx < hintsLen) {
tokenMask = collateralHints[hintsIdx++];
if (tokenMask == 0 || tokenMask & tokenMask - 1 != 0) revert InvalidCollateralHintException();
if (tokensToCheckMask & tokenMask == 0) continue;
} else {
// mask with only the LSB of `tokensToCheckMask` enabled
Expand Down
1 change: 1 addition & 0 deletions contracts/test/integration/credit/Multicall.int.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ contract MultiCallIntegrationTest is
)
);

vm.expectRevert(InvalidCollateralHintException.selector);
vm.prank(USER);
creditFacade.multicall(
creditAccount,
Expand Down
22 changes: 20 additions & 2 deletions contracts/test/unit/credit/CreditManagerV3.unit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1801,15 +1801,33 @@ contract CreditManagerV3UnitTest is TestHelper, ICreditManagerV3Events, BalanceH
//
//

/// @dev U:[CM-17]: fullCollateralCheck reverts if hf < 10K
function test_U_CM_17_fullCollateralCheck_reverts_if_hf_less_10K() public creditManagerTest {
/// @dev U:[CM-17]: fullCollateralCheck reverts with invalid params
function test_U_CM_17_fullCollateralCheck_reverts_with_invalid_params() public creditManagerTest {
vm.expectRevert(CustomHealthFactorTooLowException.selector);
creditManager.fullCollateralCheck({
creditAccount: DUMB_ADDRESS,
enabledTokensMask: 0,
collateralHints: new uint256[](0),
minHealthFactor: PERCENTAGE_FACTOR - 1
});

uint256[] memory collateralHints = new uint256[](1);
vm.expectRevert(InvalidCollateralHintException.selector);
creditManager.fullCollateralCheck({
creditAccount: DUMB_ADDRESS,
enabledTokensMask: 0,
collateralHints: collateralHints,
minHealthFactor: PERCENTAGE_FACTOR
});

collateralHints[0] = 3;
vm.expectRevert(InvalidCollateralHintException.selector);
creditManager.fullCollateralCheck({
creditAccount: DUMB_ADDRESS,
enabledTokensMask: 0,
collateralHints: collateralHints,
minHealthFactor: PERCENTAGE_FACTOR
});
}

/// @dev U:[CM-18]: fullCollateralCheck reverts if not enough collateral otherwise saves enabledTokensMask
Expand Down

0 comments on commit 5636dbe

Please sign in to comment.