Before submitting an issue on UB

Hi, I have hit what I believe is UB and since I am not using any unsafe myself, I believe this results from some issues with nalgebra. The nasal demons take the shape of a unit test which is intermittent despite not having any randomness in it, with different patterns of success depending if I run it along with other unit tests (always fails) or alone (by filtering it by name), in which case it succeeds if I println! some of the matrices involved or succeeds/fails randomly if I don’t. I haven’t had to deal with any UB in rustland until now, but this resembles what I used to see in C++ too much… The error started appearing after I refactored my code to cache some matrices inside a struct instead of recomputing them inside a repeatedly called method.

Before I open an issue on github I would like to ask what kind of information should I provide to make it the easiest for you? Because of its heisenbug behavior, it’s difficult to create a minimal repro case. It’s also not obvious if you would even see the same behavior on your machine. That being said, this is the test which is intermittent for me and it depends on whether I print my MatrixCache here or not. The always failing version is cargo test and the intermittent one is cargo test _4.

One particularly scary and suspicious operation I do is selecting columns (in from a dynamic matrix stored in a lazy_static (defined in Could it be that matrices somehow misbehave when used in lazy_static contexts?

1 Like


Thank you for reporting this problem. I have investigated a bit and I can confirm the UB comes from nalgebra. More specifically, this is due to the multiplication of a 8 × 0 matrix with a 0 × 1 vector. The case of zero-sized matrix or vector is not correctly handled by nalgebra. I’ve created an issue for this bug, including a minimal test case:

1 Like

Thank you, that was swift :slight_smile: I remember noticing this edge case at some point but I forgot to write a FIXME, so it stayed. At least now there will be a patch upstream. What are the semantics you intend to implement? For any A: n x 0 , B: 0 x m, AB == zeros(n, m)?

Yeah, I think that’s the only result that kind of makes sense. Another option would be to panic, though right now we already have other operations that return 0 on zero-shaped matrices, so I prefer to keep things coherent.