Skip to content

checked_div can attempt to negate with overflow #73

Closed
@evnu

Description

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

fn main() {
    let x = Ratio::from(-9223372036854775808_i64);
    x.checked_div(&x);
}
$ ./target/debug/repro
thread 'main' panicked at 'attempt to negate with overflow', /home/mo/.cargo/registry/src/github.com-1ecc6299db9ec823/num-traits-0.2.11/src/sign.rs:49:41
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$ RUST_BACKTRACE=1 ./target/debug/repro
...snip...
  13: core::panicking::panic
             at src/libcore/panicking.rs:52
  14: <i64 as num_traits::sign::Signed>::abs
             at /home/mo/.cargo/registry/src/github.com-1ecc6299db9ec823/num-traits-0.2.11/src/sign.rs:49
  15: <i64 as num_integer::Integer>::gcd
             at /home/mo/.cargo/registry/src/github.com-1ecc6299db9ec823/num-integer-0.1.42/src/lib.rs:478
  16: <num_rational::Ratio<T> as num_traits::ops::checked::CheckedDiv>::checked_div
             at /home/mo/.cargo/registry/src/github.com-1ecc6299db9ec823/num-rational-0.2.3/src/lib.rs:875
  17: repro::main
             at src/repro.rs:6
...snip...

This might be related to #72.

I found this with afl.rs. Setup:

use std::io::prelude::*;
use std::fs::File;

fn main() {
    let files: Vec<_> = std::env::args().skip(1).collect();
    for file in &files {
        let mut f = File::open(file).unwrap();
        let mut buf = [0; 8];
        f.read_exact(&mut buf).unwrap();

        println!("{}: {}", file, i64::from_le_bytes(buf));
    }
}

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