# Possible DoS When calling GammaTradeMarket::_removePosition will cause user position to not be able to get liquidated

GammaTradeMarket._removePosition linearly scans a user's position array, so a trader spamming tiny positions can push gas past the block limit and block liquidation of their real position.

- Payout: Audit contest
- Severity: medium
- Program: Predy
- Found by: web3km
- Disclosed: 2024-07-16
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-05-predy-findings/issues/55
- Library page: https://bugbountylibrary.com/r/c4%3A2024-05-predy%3AM-05
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:medium tech:solidity technique:source-review vuln:dos vuln:liquidation

**Target:** GammaTradeMarket / ArrayLib.removeItem (Solidity smart contract)
**Root cause:** ArrayLib.removeItem/getItemIndex linearly iterates the trader's positionIDs array to find the item; array length is attacker-controlled and unbounded, so iteration can exceed the block gas limit.
**Steps:**
1. Attacker opens many tiny low-leverage positions, each appended to positionIDs[trader].
2. Attacker opens one normal high-leverage position placed at the end of the array.
3. Liquidator calls execLiquidationCall with closeRatio == 1e18, which invokes _removePosition -> positionIDs[trader].removeItem(positionId).
4. getItemIndex loops over the huge array; gas exceeds block gas limit and the liquidation tx reverts.
**How found:** Code review of _removePosition and ArrayLib.removeItem, noticing the linear getItemIndex loop over a user-controlled array and connecting it to the execLiquidationCall path.
**Exploitation:** Griefing: attacker inflates their own positionIDs array with cheap dust positions so any full-close liquidation of their real position reverts on gas, blocking liquidators/fillers.
**Insight:** Any per-user array iterated during liquidation is a griefing surface: check whether array length is attacker-growable and whether the iteration sits on a path liquidators must traverse.

## Proof of concept (copied exactly from the original report)

**removeItem loops to find position index**

```
function removeItem(uint256[] storage items, uint256 item) internal {
        uint256 index = getItemIndex(items, item);

        removeItemByIndex(items, index);
    }
...

    function getItemIndex(uint256[] memory items, uint256 item) internal pure returns (uint256) {
        uint256 index = type(uint256).max;

        //@review - If items length is bigger, it could revert due to reaching block gas limit
        for (uint256 i = 0; i < items.length; i++) {
            if (items[i] == item) {
                index = i;
                break;
            }
        }

        return index;
    }
```

**execLiquidationCall calls _removePosition after full close**

```
function execLiquidationCall(
        uint256 vaultId,
        uint256 closeRatio,
        IFillerMarket.SettlementParamsV3 memory settlementParams
    ) external override returns (IPredyPool.TradeResult memory tradeResult) {
        tradeResult =
            _predyPool.execLiquidationCall(vaultId, closeRatio, _getSettlementDataFromV3(settlementParams, msg.sender));

        if (closeRatio == 1e18) {
@>           _removePosition(vaultId);
        }
    }
```

