Skip to content

Commit

Permalink
Prevent panics
Browse files Browse the repository at this point in the history
Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
  • Loading branch information
ggwpez committed Mar 21, 2023
1 parent a74d180 commit 7e88ac7
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions primitives/arithmetic/src/per_things.rs
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,10 @@ where
P::Inner: Into<N>,
{
let maximum: N = P::ACCURACY.into();
if part.is_zero() {
// Division by zero... best effort to prevent a panic.
return maximum
}
let c = rational_mul_correction::<N, P>(x.clone(), P::ACCURACY, part, rounding);
(x / part.into()).saturating_mul(maximum).saturating_add(c)
}
Expand Down Expand Up @@ -509,6 +513,10 @@ where
let numer_upper = P::Upper::from(numer);
let denom_n: N = denom.into();
let denom_upper = P::Upper::from(denom);
if denom.is_zero() {
// Division by zero... best effort to prevent a panic.
return N::one()
}
let rem = x.rem(denom_n);
// `rem` is less than `denom`, which fits in `P::Inner`.
let rem_inner = rem.saturated_into::<P::Inner>();
Expand Down

0 comments on commit 7e88ac7

Please sign in to comment.