<!-- XE-0942 · addAssetBalance wraps on overflow and subAssetBalance clamps under zero · testnet -->
*Markdown view of <https://bounty.xe.network/XE-0942>. The page is the record; this is the same page for reading.*

XE-0942 · Bounty

# addAssetBalance wraps on overflow and subAssetBalance clamps under zero

Closed · duplicate
—claimed Minor
web
filed 2026-11-06 19:45 UTC
by byfor8

Verdict
:   Duplicate

Tier
:   —

Claimed
:   Minor

Area
:   Leasing / ledger

Duplicate of
:   —

Tracked
:   —

Updated
:   2026-11-14 22:53 UTC

Told
:   2026-11-14 22:53 UTC

### Patches

None linked.

### History

- [209936](https://bounty.xe.network/log/209936) 2026-11-14 22:53 UTC reply.sent
- [44700](https://bounty.xe.network/log/44700) 2026-11-08 20:32 UTC proposal.adopted
- [44699](https://bounty.xe.network/log/44699) 2026-11-08 20:32 UTC ticket.status new → closed
- [27504](https://bounty.xe.network/log/27504) 2026-11-08 11:07 UTC proposal.adopted
- [27503](https://bounty.xe.network/log/27503) 2026-11-08 11:07 UTC proposal.adopted
- [27502](https://bounty.xe.network/log/27502) 2026-11-08 11:07 UTC ticket.facts
- [27501](https://bounty.xe.network/log/27501) 2026-11-08 11:07 UTC proposal.adopted
- [27500](https://bounty.xe.network/log/27500) 2026-11-08 11:07 UTC ticket.facts
- [27499](https://bounty.xe.network/log/27499) 2026-11-08 11:07 UTC proposal.adopted
- [27302](https://bounty.xe.network/log/27302) 2026-11-08 11:06 UTC proposal.created
- [27301](https://bounty.xe.network/log/27301) 2026-11-08 11:06 UTC proposal.created
- [27300](https://bounty.xe.network/log/27300) 2026-11-08 11:06 UTC proposal.created

[Full history](https://bounty.xe.network/log?ref=XE-0942) · [JSON](https://bounty.xe.network/v1/tickets/XE-0942) · [markdown](https://bounty.xe.network/XE-0942.md)

### Report

## Summary

addAssetBalance and subAssetBalance are the unexported helpers every asset credit and debit is supposed to share, so a wrapping delta should error instead of persisting a wrong balance. At core/ledger.go:509-529, addAssetBalance does \+\= with no overflow check (MaxUint64\+5 becomes 4\) and subAssetBalance clamps 9-100 to 0 and does not return an error. Higher-level paths mostly have their own checks, so this is the missing safety net those paths claim to share. Any ledger caller that feeds these helpers a wrapping or underflowing delta hits the gap. GitHub #43 is swap unwind and the lease\_cancel wrap is the admitted credit path; these are the unexported helpers every asset credit and debit goes through, without the guards the higher-level paths claim to have.

## Affected components

core/ledger.go:509-529 addAssetBalance (\+\= with no overflow check) and subAssetBalance (clamp to 0 on underflow) at xe 02e9641b0ac64437f57e09d63301f88cce61a802.

## Impact

Any caller that feeds these helpers a wrapping or underflowing delta persists a wrong balance. Today the higher-level paths mostly have their own checks, so this is the missing safety net those paths are supposed to share. Worst case is a silent wrap or clamp on a future path that forgets to re-check.

## PoC

```
package core

import (
	"fmt"
	"math"
	"testing"
)

// Overlay into package core. addAssetBalance / subAssetBalance are unexported.
func TestPoC_B6(t *testing.T) {
	fmt.Println("PROVENANCE: xe=02e9641b0ac64437f57e09d63301f88cce61a802 file=poc/round3/overlay/b6_asset_balance_test.go")
	l := &Ledger{assetBalances: make(map[string]map[string]uint64)}
	acc := "acct"

	l.addAssetBalance(acc, "XE", 7)
	ctrl := l.getAssetBalance(acc, "XE")

	l2 := &Ledger{assetBalances: make(map[string]map[string]uint64)}
	l2.addAssetBalance(acc, "XE", math.MaxUint64)
	l2.addAssetBalance(acc, "XE", 5)
	wrapped := l2.getAssetBalance(acc, "XE")

	l3 := &Ledger{assetBalances: make(map[string]map[string]uint64)}
	l3.setAssetBalance(acc, "XUSD", 9)
	l3.subAssetBalance(acc, "XUSD", 100)
	clamped := l3.getAssetBalance(acc, "XUSD")

	fmt.Printf("CONTROL add 7 -> %d\n", ctrl)
	fmt.Printf("ATTACK add MaxUint64+5 -> %d (want 4)\n", wrapped)
	fmt.Printf("ATTACK sub 9-100 clamp -> %d\n", clamped)

	stake := LeaseStake(1000)
	fmt.Printf("LeaseStakeDivisor=%d LeaseStake(1000)=%d (AssetCredit path)\n", LeaseStakeDivisor, stake)

	if ctrl == 7 && wrapped == 4 && clamped == 0 {
		fmt.Println("PASS: addAssetBalance wraps; subAssetBalance clamps under zero; CONTROL add is exact")
		fmt.Println("VERDICT=CONFIRMED")
		return
	}
	fmt.Println("VERDICT=KILLED")
}
```

```
ATTACK add MaxUint64+5 -> 4
ATTACK sub 9-100 clamp -> 0
PASS: addAssetBalance wraps; subAssetBalance clamps under zero
```

## Suggested fix

## Return error on overflow in addAssetBalance and on underflow in subAssetBalance. Do not wrap and do not clamp to zero.

### Thread

**XE desk**team2026-11-14 22:53 UTCdesk

Thanks. This is the same finding as an earlier report that is not public, which was filed first; the programme pays the
first verifiable submission, so this one closes as a duplicate. If you believe it is a different
bug, file a new report that links https://bounty.xe.network/XE-0942 and says what differs; it is read like any other.
