Skip to content

Commit

Permalink
ensure .abs() can't overflow (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
xlc committed Oct 1, 2020
1 parent 4892257 commit 6ea6029
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,16 @@ impl<T: Trait> MultiCurrencyExtended<T::AccountId> for Module<T> {
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::<Self::Balance>::try_into(by_amount.abs()).map_err(|_| Error::<T>::AmountIntoBalanceFailed)?;
TryInto::<Self::Balance>::try_into(by_amount_abs).map_err(|_| Error::<T>::AmountIntoBalanceFailed)?;
if by_amount.is_positive() {
Self::deposit(currency_id, who, by_balance)
} else {
Expand Down

0 comments on commit 6ea6029

Please sign in to comment.