georust / rstar

R*-tree spatial index for the Rust ecosystem
https://docs.rs/rstar
Apache License 2.0
415 stars 68 forks source link

Panic after inserting `Rectangle` with NaN to `RTree` #112

Open jakoschiko opened 1 year ago

jakoschiko commented 1 year ago

I wrote a program that generates random shapes and inserts them into a RTree. I accidentally generated some shapes that contained NaN and to my surprise, the program panicked. The panic occurred inside rstar.

Here a minimal example that I've tested with rstar 0.10.0

use rstar::{RTree, primitives::Rectangle};

fn main() {
    let mut tree = RTree::new();
    tree.insert(Rectangle::from_corners([-1.4, 1.0], [-1.3, 1.1]));
    tree.insert(Rectangle::from_corners([2.3, 0.0], [2.4, 0.0]));
    tree.insert(Rectangle::from_corners([-1.4, -0.4], [-1.4, -0.2]));
    tree.insert(Rectangle::from_corners([f32::NAN, f32::NAN], [f32::NAN, f32::NAN]));
    tree.insert(Rectangle::from_corners([1.8, -1.8], [1.9, -1.8]));
    tree.insert(Rectangle::from_corners([0.2, -0.5], [0.3, -0.4]));
    tree.insert(Rectangle::from_corners([-0.6, -0.6], [-0.4, -0.5])); // This will panic!
}

Output:

thread 'main' panicked at 'called `Option::unwrap()` on a `None` value', /home/explorer/.cargo/registry/src/github.com-1ecc6299db9ec823/rstar-0.10.0/src/algorithm/rstar.rs:345:14

[Rust explorer link](https://www.rustexplorer.com/b#%2F*%0A%5Bdependencies%5D%0Arstar%20%3D%20%220.10.0%22%0A*%2F%0A%0Ause%20rstar%3A%3A%7BRTree%2C%20primitives%3A%3ARectangle%7D%3B%0A%0Afn%20main()%20%7B%0A%20%20%20%20let%20mut%20tree%20%3D%20RTree%3A%3Anew()%3B%0A%20%20%20%20tree.insert(Rectangle%3A%3Afrom_corners(%5B-1.4%2C%201.0%5D%2C%20%5B-1.3%2C%201.1%5D))%3B%0A%20%20%20%20tree.insert(Rectangle%3A%3Afrom_corners(%5B2.3%2C%200.0%5D%2C%20%5B2.4%2C%200.0%5D))%3B%0A%20%20%20%20tree.insert(Rectangle%3A%3Afrom_corners(%5B-1.4%2C%20-0.4%5D%2C%20%5B-1.4%2C%20-0.2%5D))%3B%0A%20%20%20%20tree.insert(Rectangle%3A%3Afrom_corners(%5Bf32%3A%3ANAN%2C%20f32%3A%3ANAN%5D%2C%20%5Bf32%3A%3ANAN%2C%20f32%3A%3ANAN%5D))%3B%0A%20%20%20%20tree.insert(Rectangle%3A%3Afrom_corners(%5B1.8%2C%20-1.8%5D%2C%20%5B1.9%2C%20-1.8%5D))%3B%0A%20%20%20%20tree.insert(Rectangle%3A%3Afrom_corners(%5B0.2%2C%20-0.5%5D%2C%20%5B0.3%2C%20-0.4%5D))%3B%0A%20%20%20%20tree.insert(Rectangle%3A%3Afrom_corners(%5B-0.6%2C%20-0.6%5D%2C%20%5B-0.4%2C%20-0.5%5D))%3B%20%2F%2F%20This%20will%20panic!%0A%7D)

Is this intentional behavior?