# When Burning a Tokenized Position validate should be done before flipping the isLong bits in _validateAndForwardToAMM()

Burning a Panoptic tokenized position flips isLong bits before validate(), so defined-risk legs with a separate risk partner fail validation and the burn reverts permanently.

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

**Target:** SemiFungiblePositionManager / TokenId library (Solidity)
**Root cause:** _validateAndForwardToAMM() calls flipToBurnToken() (flipping isLong bits of all legs) before validate(); validate()'s risk-partner checks compare isLong of a leg against its partner, which is not flipped, so the post-flip state violates the invariants.
**Steps:**
1. Create a tokenId where a leg's riskPartner(i) != i and the two legs have equal isLong but different tokenType (passes pre-flip checks).
2. Call burnTokenizedPosition() with that tokenId.
3. _validateAndForwardToAMM() runs flipToBurnToken(), flipping isLong bits.
4. validate() then hits `((_isLong != isLongP) || _isLong == 1) && (_tokenType != tokenTypeP)` and reverts with InvalidTokenIdParameter(5).
5. Burn is permanently blocked for such positions.
**How found:** Code reading of validate()'s risk-partner logic versus the ordering in _validateAndForwardToAMM(); noticed flipToBurnToken() mutates isLong before validation, and that risk partners are not flipped together, so post-flip invariants break.
**Insight:** When a validation function checks cross-field invariants (leg vs its risk partner), any mutation applied before validation must be applied consistently to all related fields; check ordering of state transforms vs validate() in mint/burn paths.

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

**Vulnerable _validateAndForwardToAMM flips isLong before validate**

```
function _validateAndForwardToAMM(
        TokenId tokenId,
        uint128 positionSize,
        int24 tickLimitLow,
        int24 tickLimitHigh,
        bool isBurn
    ) internal returns (LeftRightUnsigned[4] memory collectedByLeg, LeftRightSigned totalMoved) {
        // Reverts if positionSize is 0 and user did not own the position before minting/burning
        if (positionSize == 0) revert Errors.OptionsBalanceZero();


        /// @dev the flipToBurnToken() function flips the isLong bits
        if (isBurn) {
            tokenId = tokenId.flipToBurnToken();
        }


        // Validate tokenId
        tokenId.validate();


        // Extract univ3pool from the poolId map to Uniswap Pool
        IUniswapV3Pool univ3pool = s_poolContext[tokenId.poolId()].pool;


        // Revert if the pool not been previously initialized
        if (univ3pool == IUniswapV3Pool(address(0))) revert Errors.UniswapPoolNotInitialized();
```

**validate() risk partner isLong checks**

```
uint256 _isLong = self.isLong(i);
                    uint256 isLongP = self.isLong(riskPartnerIndex);


                    // token type status of associated legs (call/put)
                    uint256 _tokenType = self.tokenType(i);
                    uint256 tokenTypeP = self.tokenType(riskPartnerIndex);


                    // if the position is the same i.e both long calls, short put's etc.
                    // then this is a regular position, not a defined risk position
                    if ((_isLong == isLongP) && (_tokenType == tokenTypeP))
                        revert Errors.InvalidTokenIdParameter(4);


                    // if the two token long-types and the tokenTypes are both different (one is a short call, the other a long put, e.g.), this is a synthetic position
                    // A synthetic long or short is more capital efficient than each leg separated because the long+short premia accumulate proportionally
                    // unlike short stranlges, long strangles also cannot be partnered, because there is no reduction in risk (both legs can earn premia simultaneously)
                    if (((_isLong != isLongP) || _isLong == 1) && (_tokenType != tokenTypeP))
                        revert Errors.InvalidTokenIdParameter(5);
```

