Skip to content

fix(order): use checked arithmetic in place_order to prevent integer overflow [CO-01]#289

Merged
Leothosine merged 1 commit into
tosirano:mainfrom
yusuftomilola:fix/co-01-integer-overflow-order-total
Jun 24, 2026
Merged

fix(order): use checked arithmetic in place_order to prevent integer overflow [CO-01]#289
Leothosine merged 1 commit into
tosirano:mainfrom
yusuftomilola:fix/co-01-integer-overflow-order-total

Conversation

@yusuftomilola

Copy link
Copy Markdown
Contributor

Summary

Fixes the unchecked arithmetic in place_order() that allowed silent integer overflow when quantity or unit_price were large values.

  • Replaced item.unit_price * item.quantity as i128 with checked_mul + checked_add
  • Added MAX_ITEM_UNIT_PRICE (1T stroops) and MAX_ORDER_TOTAL (100T stroops) boundary constants validated before and during the loop
  • Added tests: overflow with u32::MAX quantity, correct multi-item totals, max-total boundary, unit price cap rejection

Changes

  • contracts/order/src/lib.rs — checked arithmetic and boundary guards in place_order

Closes #279

Fixes CO-01 as specified in issue tosirano#279:
- Replace unchecked * and += with checked_mul / checked_add
- Add MAX_ITEM_UNIT_PRICE and MAX_ORDER_TOTAL boundary constants
- Validate unit_price and running total against constants before loop proceeds
- Add tests: overflow with u32::MAX, correct multi-item total, max total boundary
@Leothosine Leothosine merged commit cf212b1 into tosirano:main Jun 24, 2026
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CO-01] Fix integer overflow in order total calculation - place_order uses unchecked arithmetic

2 participants