# _ensureMaxLoops causes liquidateAccount to fail in certain conditions

Venus Comptroller's maxLoopsLimit blocks liquidateAccount when a borrower spans many markets, since full liquidation needs up to 2n-1 orders, leaving bad debt unliquidatable.

- Payout: Contest reward
- Severity: medium
- Program: Venus Protocol
- Credited: xuwinnie
- Disclosed: 2023-08-09
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2023-05-venus-findings/issues/327
- Library page: https://bugbountylibrary.com/r/c4%3A2023-05-venus%3AM-04
- Tags: domain:web3 impact:dos impact:funds-loss kind:contest platform:code4rena sev:medium tech:evm tech:solidity vuln:business-logic vuln:dos vuln:liquidation

**Target:** Comptroller.liquidateAccount / _ensureMaxLoops
**Root cause:** _ensureMaxLoops caps orderCount at maxLoopsLimit, but a complete liquidation across n markets can require up to 2n-1 orders (multiple orders per borrowed asset when collateral/debt values don't match one-to-one), exceeding the limit and reverting.
**Steps:**
1. Set maxLoopsLimit=16; add 12 markets (allowed since marketsCount=12<=16).
2. Borrower enters all 12 markets via enterMarkets (accountAssetsLen+len<=16).
3. Borrower deposits ~32.1 USD across 12 stablecoins and borrows ~24 USD across them.
4. USDA de-pegs to 0.6, dropping deposit value to ~24.1 USD below liquidation threshold and minLiquidatableCollateral.
5. Call `liquidateAccount`; minimum orderCount is 23 (2n-1) > maxLoopsLimit=16, so `_ensureMaxLoops` reverts and liquidation is impossible.
**How found:** Reasoned about the interaction between the maxLoopsLimit cap and the worst-case order count for liquidateAccount, noting collateral and debt values rarely match one-to-one so multiple orders per borrowed asset are needed; proved worst case is 2n-1 orders.
**Insight:** When a loop-limit guard is shared between configuration functions (addMarket/enterMarkets) and liquidation paths, check whether the liquidation path's worst-case iteration count can exceed the same cap that config functions were allowed to approach—leaving positions unliquidatable.
