From 6ea602996e90182b4842acb1f71da1fa81a9d00d Mon Sep 17 00:00:00 2001 From: Xiliang Chen Date: Thu, 1 Oct 2020 16:27:43 +1300 Subject: [PATCH] ensure .abs() can't overflow (#298) --- tokens/src/lib.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tokens/src/lib.rs b/tokens/src/lib.rs index a7ddbfb5b..861b4c3cd 100644 --- a/tokens/src/lib.rs +++ b/tokens/src/lib.rs @@ -474,8 +474,16 @@ impl MultiCurrencyExtended for Module { return Ok(()); } + // Ensure this doesn't overflow. There isn't any traits that exposes + // `saturating_abs` so we need to do it manually. + let by_amount_abs = if by_amount == Self::Amount::min_value() { + Self::Amount::max_value() + } else { + by_amount.abs() + }; + let by_balance = - TryInto::::try_into(by_amount.abs()).map_err(|_| Error::::AmountIntoBalanceFailed)?; + TryInto::::try_into(by_amount_abs).map_err(|_| Error::::AmountIntoBalanceFailed)?; if by_amount.is_positive() { Self::deposit(currency_id, who, by_balance) } else {