Skip to content

Commit 6ea6029

Browse files
authored
ensure .abs() can't overflow (#298)
1 parent 4892257 commit 6ea6029

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

tokens/src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,16 @@ impl<T: Trait> MultiCurrencyExtended<T::AccountId> for Module<T> {
474474
return Ok(());
475475
}
476476

477+
// Ensure this doesn't overflow. There isn't any traits that exposes
478+
// `saturating_abs` so we need to do it manually.
479+
let by_amount_abs = if by_amount == Self::Amount::min_value() {
480+
Self::Amount::max_value()
481+
} else {
482+
by_amount.abs()
483+
};
484+
477485
let by_balance =
478-
TryInto::<Self::Balance>::try_into(by_amount.abs()).map_err(|_| Error::<T>::AmountIntoBalanceFailed)?;
486+
TryInto::<Self::Balance>::try_into(by_amount_abs).map_err(|_| Error::<T>::AmountIntoBalanceFailed)?;
479487
if by_amount.is_positive() {
480488
Self::deposit(currency_id, who, by_balance)
481489
} else {

0 commit comments

Comments
 (0)