A bad-debt write-off in QieForgeVault.liquidate() shrank totalDebtPrincipal
(the vault's real backing) without ever touching the separate, already-
realized protocolReserves bucket. With a real nonzero reserveFactorBps
(every real deploy uses one), a position whose entire debt is written off
could leave protocolReserves larger than totalDebtPrincipal + idle balance
combined, making totalAssets()'s subtraction underflow-revert forever and
permanently freezing every lender's deposit()/withdraw(). liquidate() now
clamps protocolReserves down to the vault's actual post-write-off backing,
taking only the excess beyond lenders' own cushion out of realized
treasury revenue.
Verified via 2 new Foundry regression tests (62 -> 64/64 pass, zero
regressions) and live on real deployed bytecode: a new one-command demo
(scripts/bad-debt-reserve-clamp-demo.sh) against a disposable anvil, and
re-proven on the persistent self-hosted chain (130.61.180.42:8647) via a
throwaway vault, tx 0xc4cdb5cb99a1340586eecee63be8d10bf6c673a6613cfead6a4cef3f9adaae08.
A second, distinct division-by-zero edge case in deposit() (when
totalAssets()==0 but shares are still outstanding) was found live while
building the demo and is intentionally left for a future pass.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>