Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i32 overflow when determining SVD result size #313

Open
jgarthur opened this issue May 25, 2022 · 1 comment
Open

i32 overflow when determining SVD result size #313

jgarthur opened this issue May 25, 2022 · 1 comment

Comments

@jgarthur
Copy link

jgarthur commented May 25, 2022

The code linked below can overflow when any of the products is above i32::MAX, potentially leading to a much larger memory allocation than intended (example). There are a few other instances in other calls to vec_uninit

https://github.com/rust-ndarray/ndarray-linalg/blob/master/lax/src/svddc.rs#L44-L49

@sjackman
Copy link

sjackman commented May 25, 2022

https://play.rust-lang.org/?gist=501fb52b2beb650d2ac9ec6872f269e9

#[allow(arithmetic_overflow)]
fn main() {
    let m: i32 = 2_000;
    let n: i32 = 1_080_000;
    println!("{}", m * n);
    println!("{}", (m * n) as usize);
    println!("{}", m as usize * n as usize);
}

Run in debug mode, it panics.

thread 'main' panicked at 'attempt to multiply with overflow', src/main.rs:5:20

Run in release mode, silent arithmetic integer overflow.

-2134967296
18446744071574584320
2160000000

All occurrences of (x * y) as usize where x and y are i32 ought to be x as usize * y as usize. When x and y are i32 and known to be positive, x as usize * y as usize cannot overflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants