Dev.to Security 🔐 Cybersecurity 👁 0 📖 2 min read

The subtraction that bricked every future investment — TTSwap's getInvestPower underflow

The subtraction that bricked every future investment — TTSwap's getInvestPower underflow TTSwap's bounty sweep wrapped up this week with nine independently QA-verified fixes. This one (PR #339, bounty #328) is a good e

The subtraction that bricked every future investment — TTSwap's getInvestPower underflow

TTSwap's bounty sweep wrapped up this week with nine independently QA-verified fixes. This one (PR #339, bounty #328) is a good example of why "it compiles and the tests pass" is not the same as "the math is safe": a single unchecked subtraction assumption turned a normal trading state into a permanent revert for every later investGood call.

The bug class

L_Good.getInvestPower prices how much power a new investment gets. It estimates unencumbered pool depth like this:

uint128 virtual_quantity = config.amount1();        // leverage virtualQty
uint128 current_quantity = _self.currentState.amount1() - virtual_quantity;

The assumption baked in: amount1 (Q, the quote-side balance) can never fall below virtualQty. But swaps reduce Q and never touch virtualQty — and the safe-line floor only bounds Q at roughly 60% of baseline, not at virtualQty. A max-size swap-out, reachable after a token donation inflates the pool's apparent depth, pushes Q below virtualQty. From then on, Q - virtualQty underflows under Solidity 0.8 checked math and reverts Panic(0x11) — inside a helper that every investGood call depends on. Trading doesn't just get risky; investment is bricked.

The PoC is precise about it: Q at 1,902,398,400 against a virtualQty of 2,000,398,400 — a ~5% gap is all it takes.

The fix pattern

Saturate instead of subtracting blindly, and treat an empty side as minimum power rather than dividing by it:

uint128 q = _self.currentState.amount1();
uint128 current_quantity = q > virtual_quantity ? q - virtual_quantity : 0;
if (current_quantity == 0 || invest_quantity == 0) {
    limitpower_ = 100;
} else if (current_quantity < invest_quantity) {
    // ...existing proportional clamp
}

The fix's comment says the quiet part out loud: "Swaps reduce Q but never virtualQty, so Q can legally fall below virtualQty." The precondition wasn't enforced anywhere — it was just assumed.

Why skeptical devs respect this

Independent QA reran it clean: base reproduces the exact Panic(0x11); the fix branch passes 2/2 dedicated tests under pinned solc 0.8.29. The submission's "100/100 focused regression tests" claim was not fully rerun by QA, so that stays UNVERIFIED — the dedicated PoC is what carries the verdict. Same discipline we apply to our own product pages: only what was executed gets stated as fact.

The meta-lesson

Any invariant of the form "X is always ≥ Y" needs a mechanism that enforces it, not a comment that hopes for it. Here the mechanism existed for a different bound (the ~60% safe-line floor) and nobody checked whether it implied the tighter one the subtraction needed. When you write a - b, ask: what proves a >= b on every reachable path — including after donations, swaps, and config changes?

Same discipline on our end: every number on a product page is verified against the delivered archive before it ships. Free tools and the auto-updating store link are on the nexusfoundry dev.to profile.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.