# Division before multiplication could lead to users losing 50% in WithdrawalQueue

WithdrawalQueue._getAvailable divides before multiplying, truncating withdrawable-per-share to 1 and locking ~50% of a user's funds.

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

**Target:** WithdrawalQueue Solidity contract (_getAvailable/_getWithdrawablePerShare)
**Root cause:** _getWithdrawablePerShare() computes (totalWithdrawn + balance) / totalShares, truncating to an integer before it is multiplied by getShares[_tokenId], so per-share precision is lost.
**Steps:**
1. Set state: getShares[_tokenId]=1e8, getWithdrawn=0, _totalWithdrawn=0, balance=1e9, getTotalShares=5e8+1.
2. Call _getAvailable: _getWithdrawablePerShare()=1e9/(5e8+1)=1 (truncated).
3. Result: 1e8*1-0=1e8 withdrawable.
4. Correct value: 1e8*1e9/(5e8+1)=199999999; user loses ~50%.
5. Fix: multiply getShares by balance before dividing by totalShares.
**How found:** Code review of the withdrawable math spotted division before multiplication in _getWithdrawablePerShare, then a numeric example quantified the truncation.
**Exploitation:** Any user withdrawing from a queue whose totalShares slightly exceeds balance-per-share granularity receives a truncated amount; the remainder stays locked in the contract.
**Insight:** In share/asset accounting, always multiply by the holder's shares before dividing by totalShares; a per-share helper that divides first silently floors to 1 whenever balance < totalShares.

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

**Vulnerable _getAvailable and _getWithdrawablePerShare functions**

```
function _getAvailable(uint256 _tokenId) private view returns (uint256) {
    return getShares[_tokenId] * _getWithdrawablePerShare() - getWithdrawn[_tokenId]; 
}

/// @notice Get the amount that can be withdrawn per share.
function _getWithdrawablePerShare() private view returns (uint256) {
    return (_totalWithdrawn + _asset.balanceOf(address(this))) / getTotalShares;
```

**PoC scenario showing 50% loss**

```
getShares[_tokenId] = 1e8
getWithdrawn[_tokenId] = 0
_totalWithdrawn = 0
_asset.balanceOf(address(this)) = 1e9 (1000 USDC)
getTotalShares = 5e8 + 1

The current calculation will yield:

_getWithdrawablePerShare() = 1e9 / (5e8 + 1) = 1
_getAvailable() = 1e8 * 1 - 0 = 1e8 = 100000000

However, the users should actually receive:

getShares[_tokenId] * _asset.balanceOf(address(this)) / getTotalShares
= 1e8 * 1e9 / (5e8 + 1) = 199999999
```

