# voters cannot disable max lock

Velocimeter VotingEscrow disable_max_lock miscomputes maxLockIdToIndex when removing the last array element, leaving stale index and blocking withdrawal.

- Payout: Audit contest
- Severity: high
- Program: Velocimeter
- Found by: 0xpiken
- Disclosed: 2024-06-27
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-06-velocimeter-judging/issues/257
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-06-velocimeter%3AH-8
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** VotingEscrow.sol disable_max_lock / max_locked_nfts array
**Root cause:** When disabling max lock on the last element of max_locked_nfts, the code writes maxLockIdToIndex[max_locked_nfts[index]] = index+1 after overwriting the slot with the last element, so the removed token's index is never zeroed and the swapped element's index is set incorrectly.
**Steps:**
1. Create three locks via `escrow.create_lock(400, lockDuration)` producing tokenIds 1,2,3.
2. Call `escrow.enable_max_lock(1)`, `enable_max_lock(2)`, `enable_max_lock(3)`; max_locked_nfts=[1,2,3], maxLockIdToIndex={1:1,2:2,3:3}.
3. Call `escrow.disable_max_lock(3)` (last element).
4. Observe maxLockIdToIndex[3] remains 3 instead of 0, and max_locked_nfts shrinks to [1,2].
5. Subsequent disable/withdraw logic for token 3 is corrupted, blocking withdrawal.
**How found:** Manual review of the array-swap deletion logic in disable_max_lock; noticed the index recomputation uses max_locked_nfts[index] after it was overwritten with the last element, so the removed token's mapping is never cleared.
**Insight:** In swap-and-pop array deletion, verify the mapping update targets the token that was moved into the vacated slot, not the slot's new value; test removing the last element specifically, since that path skips the swap and exposes stale-index bugs.

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

**Foundry test enabling then disabling max lock**

```
function testEnableAndDisableMaxLock() external {
        flowDaiPair.approve(address(escrow), TOKEN_1);
        uint256 lockDuration = 7 * 24 * 3600; // 1 week
        escrow.create_lock(400, lockDuration);
        escrow.create_lock(400, lockDuration);
        escrow.create_lock(400, lockDuration);

        assertEq(escrow.currentTokenId(), 3);
        escrow.enable_max_lock(1);
        escrow.enable_max_lock(2);
        escrow.enable_max_lock(3);


        assertEq(escrow.maxLockIdToIndex(1), 1);
        assertEq(escrow.maxLockIdToIndex(2), 2);
        assertEq(escrow.maxLockIdToIndex(3), 3);

        assertEq(escrow.max_locked_nfts(0), 1);
        assertEq(escrow.max_locked_nfts(1), 2);
        assertEq(escrow.max_locked_nfts(2), 3);

        escrow.disable_max_lock(3);

        assertEq(escrow.maxLockIdToIndex(1), 1);
        assertEq(escrow.maxLockIdToIndex(2), 2);
        assertEq(escrow.maxLockIdToIndex(3), 3);//mockLockIdToIndex has to be zero 

        assertEq(escrow.max_locked_nfts(0), 1);
        assertEq(escrow.max_locked_nfts(1), 2);
    }
```

