Skip to content

Stackoverflow with PartialEq and PartialOrd #140

@redweasel

Description

@redweasel

The following code produces a stackoverflow, which kills the process.

use num_rational::*; // 0.4.2
use num_bigint::*; // 0.4.6
use num_traits::One;

fn main() {
    let mut a = BigRational::new("29".parse().unwrap(), "28".parse().unwrap());
    for _ in 0..13 {
        a = &a * &a;
    }
    let b = Ratio::new_raw(a.numer() + &BigInt::one(), a.denom() + &BigInt::one());
    assert!(a > b);
    assert!(a != b);
}

If anyone uses this on a server where the numbers can be user supplied, this would lead to a DoS, so this is security critical. The fix is to write the continued fractions algorithm for the comparison in an iterative instead of a recursive way.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions