AtheMathmo / rulinalg

A linear algebra library written in Rust

Home Page:https://crates.io/crates/rulinalg

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`swap_rows()` invokes undefined behavior

Andlon opened this issue · comments

If you call

matrix.swap_rows(index, index);

I believe it currently invokes undefined behavior, as it will perform mem::swap on elements that reside in the same memory location. The same is currently not true for swap_cols(), as it uses ptr::swap.

Moreover, even if this might work with the current Rust compiler, it still performs a full swap when we could let it do a no-op. If swap_rows(index, index) was a no-op, we could simplify some calling code which currently explicitly checks that we're not swapping a row with itself (see for example the lup_decomp code). This is also true for swap_cols().

I think a fix should at the very least only perform the swap if a != b in swap_rows() and swap_cols(). Even better if we can rewrite it to avoid unsafe code without a performance penalty (I don't know if this is doable).