Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 7e88ac7

Browse files
committed
Prevent panics
Signed-off-by: Oliver Tale-Yazdi <[email protected]>
1 parent a74d180 commit 7e88ac7

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

primitives/arithmetic/src/per_things.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ where
479479
P::Inner: Into<N>,
480480
{
481481
let maximum: N = P::ACCURACY.into();
482+
if part.is_zero() {
483+
// Division by zero... best effort to prevent a panic.
484+
return maximum
485+
}
482486
let c = rational_mul_correction::<N, P>(x.clone(), P::ACCURACY, part, rounding);
483487
(x / part.into()).saturating_mul(maximum).saturating_add(c)
484488
}
@@ -509,6 +513,10 @@ where
509513
let numer_upper = P::Upper::from(numer);
510514
let denom_n: N = denom.into();
511515
let denom_upper = P::Upper::from(denom);
516+
if denom.is_zero() {
517+
// Division by zero... best effort to prevent a panic.
518+
return N::one()
519+
}
512520
let rem = x.rem(denom_n);
513521
// `rem` is less than `denom`, which fits in `P::Inner`.
514522
let rem_inner = rem.saturated_into::<P::Inner>();

0 commit comments

Comments
 (0)