From 27f5881553393175d2feac57b41ac9e20406bead Mon Sep 17 00:00:00 2001 From: Luke Parker Date: Fri, 20 Jan 2023 08:08:20 -0500 Subject: [PATCH] Ensure Amount uses checked ops --- substrate/serai/primitives/src/amount.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/substrate/serai/primitives/src/amount.rs b/substrate/serai/primitives/src/amount.rs index e3f0fcaf..daf40abe 100644 --- a/substrate/serai/primitives/src/amount.rs +++ b/substrate/serai/primitives/src/amount.rs @@ -1,6 +1,4 @@ -use core::{ - ops::{Add, Mul}, -}; +use core::ops::{Add, Sub, Mul}; use scale::{Encode, Decode, MaxEncodedLen}; use scale_info::TypeInfo; @@ -21,13 +19,21 @@ pub const COIN: Amount = Amount(1_000_000_00); impl Add for Amount { type Output = Amount; fn add(self, other: Amount) -> Amount { - Amount(self.0 + other.0) + // Explicitly use checked_add so even if range checks are disabled, this is still checked + Amount(self.0.checked_add(other.0).unwrap()) + } +} + +impl Sub for Amount { + type Output = Amount; + fn sub(self, other: Amount) -> Amount { + Amount(self.0.checked_sub(other.0).unwrap()) } } impl Mul for Amount { type Output = Amount; fn mul(self, other: Amount) -> Amount { - Amount(self.0 * other.0) + Amount(self.0.checked_mul(other.0).unwrap()) } }