Skip to content

Arithmetic hardening: saturating_add on earnings and next_event_id should be checked #72

Description

@0xdevcollins

Severity: LOW · Contract · boundless-profile + boundless-events

Surfaced by the Almanax scan of e1f17937. Closes Earnings overflow silently clamps to i128 max and Saturating event-id counter can freeze at u64 max.

Files

  • contracts/profile/src/earnings.rs:28-30register earnings saturating_add
  • contracts/events/src/idempotency.rs:33-38next_event_id double saturating_add

Problem

  • Earnings accumulate with saturating_add, which silently pins a (user, token) value at i128::MAX forever on overflow → irrecoverable accounting drift; later increments are no-ops.
  • next_event_id saturating at u64::MAX freezes the counter, returning the same id forever → id collisions / event-state aliasing.

Fix

Use checked arithmetic that reverts with a typed error:

  • current.checked_add(amount).ok_or(Error::EarningsOverflow)?
  • id.checked_add(1).ok_or(Error::EventIdOverflow)? (and the stored next-id)

This aligns with the repo hard rule ("no unwrap on host Option/Result; return a typed Error"). Reverting is the correct failure mode for both.

Tests

  • Overflow inputs return the typed error rather than clamping.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions