# FlashSwapRouter::emptyReserve()  and FlashSwapROuter::emptyReservePartial() functions return incorrect values

FlashSwapRouter.emptyReserve/emptyReservePartial return post-subtraction reserve instead of amount emptied, causing wrong PSM redemption and locked RA on final LV redeem.

- Payout: Audit contest
- Severity: high
- Program: Cork Protocol
- Found by: 0x73696d616f
- Disclosed: 2024-08-22
- Write-up quality: 7/10
- Original: https://github.com/sherlock-audit/2024-08-cork-protocol-judging/issues/68
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-08-cork-protocol%3AH-2
- Tags: area:web3 domain:web3 impact:dos impact:funds-loss kind:contest platform:sherlock sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** FlashSwapRouter / VaultLib LV redemption and liquidation
**Root cause:** emptyReservePartial returns self.ds[dsId].reserve after subtracting amount, i.e. the remaining reserve, not the amount emptied; callers treat it as the emptied amount.
**Steps:**
1. Deploy protocol, mint LV via `moduleCore.depositLv(id, 10e18)`
2. Approve LV to moduleCore
3. Call `moduleCore.redeemEarlyLv(id, alice, 10e18)`
4. Observe revert `TransferHelper::transferFrom: transferFrom failed` because ctSellAmount computed from wrong reserve exceeds CT balance
**How found:** Code reading of VaultLib._liquidatedLp and _redeemCtDsAndSellExcessCt against documented intent (excess DS paired with CT); noticed emptyReservePartial returns remaining reserve rather than emptied amount, then traced downstream arithmetic and wrote a Foundry PoC.
**Exploitation:** Not an external attack; triggered by legitimate user redemption. When reservedDs≈ammCtBalance, emptyReservePartial returns ~0, so ctSellAmount = ammCtBalance - 0 is far larger than actual CT held, the AMM swap transferFrom reverts, and the last LV holders cannot redeem, locking RA. In liquidation path, emptyReserve returning 0 makes lvRedeemRaWithCtDs redeem 0 and routes all CT to lvRedeemRaPaWithCt, over-claiming PA.
**Insight:** When a helper named empty*/withdraw* returns a value, verify whether it returns the amount removed or the remaining balance; callers often assume the former, and the mismatch only surfaces at boundary conditions (last withdrawal, reserve≈balance).

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

**Test proving redeemEarlyLv reverts due to incorrect reserve**

```
function test_IncorrectEmptyReserveReturnedValue() public {
        vm.startPrank(alice);
        WETH.mint(alice, 10e18);
        WETH.approve(address(moduleCore), type(uint256).max);
        moduleCore.depositLv(id, 10e18);
        Asset(lvAddress).approve(address(moduleCore), type(uint256).max);
        vm.expectRevert(bytes("TransferHelper::transferFrom: transferFrom failed"));
        moduleCore.redeemEarlyLv(id, alice, 10e18);
        vm.stopPrank();
    }
```

**emptyReserve/emptyReservePartial returning remaining reserve**

```
function emptyReserve(ReserveState storage self, uint256 dsId, address to) internal returns (uint256 reserve) {
        reserve = emptyReservePartial(self, dsId, self.ds[dsId].reserve, to);
    }

    function emptyReservePartial(ReserveState storage self, uint256 dsId, uint256 amount, address to)
        internal
        returns (uint256 reserve)
    {
        self.ds[dsId].ds.transfer(to, amount);

        self.ds[dsId].reserve -= amount;
        reserve = self.ds[dsId].reserve;
    }
```

