# Fragmentation fee is not taken if user compensates with newly created position

Size compensate() with RESERVED_ID creates a new credit position equal to amountToCompensate, making exiterCreditRemaining zero and skipping the fragmentation fee.

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

**Target:** Compensate.sol action library (credit/debt positions)
**Root cause:** When compensating with RESERVED_ID, the new credit position is created with credit == amountToCompensate, so exiterCreditRemaining = credit - amountToCompensate is always 0, and the fragmentation fee branch is never entered.
**Steps:**
1. Call `compensate` with `creditPositionToCompensateId = RESERVED_ID` and `amount = creditPositionWithDebtToRepay.credit / 2`.
2. `createDebtAndCreditPositions` sets new credit = futureValue = amountToCompensate.
3. `exiterCreditRemaining = creditPositionToCompensate.credit - amountToCompensate` evaluates to 0.
4. `if (exiterCreditRemaining > 0)` is false, so fragmentation fee transferFrom is skipped.
5. Debt credit is split without paying the fragmentation fee.
**How found:** Code review of Compensate.sol: noticed the new position's credit is set to amountToCompensate, making the exiterCreditRemaining subtraction trivially zero and bypassing the fee condition.
**Exploitation:** User splits their debt credit in half by compensating with a freshly created position, avoiding the fragmentation fee that would normally be charged on credit fractionalization.
**Insight:** When a fee is gated on a computed remainder, check whether the branch that creates the counterpart position sets its size equal to the subtracted amount, zeroing the remainder by construction.

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

**Compensate function creating new credit position**

```
function executeCompensate(State storage state, CompensateParams calldata params) external {
        emit Events.Compensate(
            params.creditPositionWithDebtToRepayId, params.creditPositionToCompensateId, params.amount
        );

        CreditPosition storage creditPositionWithDebtToRepay =
            state.getCreditPosition(params.creditPositionWithDebtToRepayId);
        DebtPosition storage debtPositionToRepay =
            state.getDebtPositionByCreditPositionId(params.creditPositionWithDebtToRepayId);

>>      uint256 amountToCompensate = Math.min(params.amount, creditPositionWithDebtToRepay.credit);

        CreditPosition memory creditPositionToCompensate;
        if (params.creditPositionToCompensateId == RESERVED_ID) {
>>          creditPositionToCompensate = state.createDebtAndCreditPositions({
                lender: msg.sender,
                borrower: msg.sender,
                futureValue: amountToCompensate,
                dueDate: debtPositionToRepay.dueDate
            });
```

**exiterCreditRemaining zero skips fragmentation fee**

```
uint256 exiterCreditRemaining = creditPositionToCompensate.credit - amountToCompensate;

        // credit emission
        state.createCreditPosition({
            exitCreditPositionId: params.creditPositionToCompensateId == RESERVED_ID
                ? state.data.nextCreditPositionId - 1
                : params.creditPositionToCompensateId,
            lender: creditPositionWithDebtToRepay.lender,
            credit: amountToCompensate
        });
>>      if (exiterCreditRemaining > 0) {
            // charge the fragmentation fee in collateral tokens, capped by the user balance
            uint256 fragmentationFeeInCollateral = Math.min(
                state.debtTokenAmountToCollateralTokenAmount(state.feeConfig.fragmentationFee),
                state.data.collateralToken.balanceOf(msg.sender)
            );
            state.data.collateralToken.transferFrom(
                msg.sender, state.feeConfig.feeRecipient, fragmentationFeeInCollateral
            );
```

