# Incorrect circular array check in _updatePendingWithdrawalWithQueue flow, causing received funds to be added to the wrong queues

Off-by-one circular-array break condition in Gondi Pool's queueClaimAll misroutes or deletes received loan funds across withdrawal queues, freezing/misallocating funds.

- Payout: Audit contest
- Severity: high
- Program: Gondi
- Found by: oakcobalt
- Disclosed: 2024-07-25
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2024-04-gondi-findings/issues/47
- Library page: https://bugbountylibrary.com/r/c4%3A2024-04-gondi%3AH-08
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** Pool.sol withdrawal queue accounting (_updatePendingWithdrawalWithQueue / queueClaimAll)
**Root cause:** The loop break uses `secondIdx == _cachedPendingQueueIndex + 1` without `% totalQueues`, so on circular queue wraparound it either breaks immediately (skipping the oldest queue) or never breaks (distributing to older queues).
**Steps:**
1. Note queues are circular, indices wrap at getMaxTotalWithdrawalQueues.
2. In _updatePendingWithdrawalWithQueue, second for-loop breaks on `secondIdx == _cachedPendingQueueIndex + 1` (no modulo).
3. Case 1: _cachedPendingQueueIndex < max, oldestQueueIndex == _cachedPendingQueueIndex+1 -> immediate break; getTotalReceived[oldestQueueIndex] is zeroed without distribution.
4. Case 2: _cachedPendingQueueIndex == max -> condition never true; loop runs max+1 times distributing to older queues too.
5. Fix: `if (i != 0 && secondIdx == (_cachedPendingQueueIndex + 1) % totalQueues) break;`
**How found:** Code review of the circular queue index arithmetic in _updatePendingWithdrawalWithQueue, noticing the break condition lacked the `% totalQueues` used elsewhere for circular indices.
**Exploitation:** Not an attacker-driven exploit; accounting bug triggered by normal queueClaimAll flow causing received funds to be misallocated or deleted, potentially freezing funds.
**Insight:** When auditing circular/ring-buffer indices, verify every index comparison and break condition applies the same modulo used for advancement; a single un-modded comparison flips loop termination at wraparound boundaries.

