# Miscalculation in _farmPlots function could lead to a user unable to unstake all NFTs

Integer division order in _farmPlots makes schnibblesTotal negative when finalBonus<0, underflowing to near uint256 max and reverting unstakeMunchable, locking NFTs.

- Payout: Audit contest
- Severity: high
- Program: Munchables
- Found by: KaligoAudits
- Disclosed: 2024-08-16
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-07-munchables-findings/issues/80
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-munchables%3AH-03
- Tags: area:web3 domain:web3 impact:dos kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:dos

**Target:** LandManager _farmPlots / unstakeMunchable (Solidity contract)
**Root cause:** schnibblesTotal is computed as uint256((int256(s) + int256(s)*finalBonus)/100); when finalBonus is negative the sum is negative, and casting to uint256 underflows to near type(uint256).max.
**Steps:**
1. Register alice with realm 1 (negative REALM_BONUSES) and stake a Common/Primordial Munchable in a plot whose snuggeryRealm yields finalBonus < 0.
2. Wait so lastToilDate is in the past.
3. Call `unstakeMunchable()`; forceFarmPlots modifier invokes `_farmPlots()`.
4. `schnibblesTotal` becomes negative, cast to uint256 wraps to ~max.
5. `schnibblesLandlord = schnibblesTotal * latestTaxRate / 1e18` overflows and reverts, blocking unstake.
**How found:** Code review of _farmPlots reward math: noticed uint256 cast of an expression that can be negative when finalBonus<0, then traced downstream multiplication by latestTaxRate to confirm overflow revert.
**Escalation:** All previously staked Munchables in the same call path are blocked from unstaking, not just the affected NFT.
**Insight:** When a signed bonus is applied to an unsigned accumulator, check whether the cast wraps before division; a negative intermediate cast to uint256 becomes huge and any subsequent multiply overflows, turning a math bug into a DoS on the modifier path.

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

**Test diff setting alice realm and bob reveal to trigger revert**

```
await registerPlayer({
   account: alice,
+  realm: 1,
   testContracts,
});
await registerPlayer({
   account: bob,
   testContracts,
});
await registerPlayer({
   account: jirard,
   testContracts,
});
.
.
.
await mockNFTOverlord.write.addReveal([alice, 100], { account: alice });
   await mockNFTOverlord.write.addReveal([bob, 100], { account: bob });
   await mockNFTOverlord.write.addReveal([bob, 100], { account: bob });
   await mockNFTOverlord.write.startReveal([bob], { account: bob }); // 1
-  await mockNFTOverlord.write.reveal([bob, 4, 12], { account: bob }); // 1
+  await mockNFTOverlord.write.reveal([bob, 1, 0], { account: bob }); // 1
   await mockNFTOverlord.write.startReveal([bob], { account: bob }); // 2
   await mockNFTOverlord.write.reveal([bob, 0, 13], { account: bob }); // 2
   await mockNFTOverlord.write.startReveal([bob], { account: bob }); // 3
```

**Vulnerable schnibblesTotal calculation with finalBonus**

```
schnibblesTotal =
        (timestamp - _toiler.lastToilDate) *
        BASE_SCHNIBBLE_RATE;
schnibblesTotal = uint256(
    (int256(schnibblesTotal) +
        (int256(schnibblesTotal) * finalBonus)) / 100
    );
```

