Skip to content

checked_div can divide by zero #72

@turion

Description

@turion

It's possible to panic with a zero division by using checked_div.

#![allow(dead_code)]
#![warn(clippy::pedantic)]

use num_rational::Ratio;
use num_traits::ops::checked::CheckedDiv;

fn main() {
    #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
    let x = Ratio::from(0);
    dbg!(x.checked_div(&x));
}

Playground

The reason seems to be that the gcds are divided unsafely:

.checked_mul(&(rhs.numer.clone() / gcd_ac.clone()))?;

Probably, one should use otry!(checked_div(...) or something similar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions