Skip to content

JacobiSVD::solve() crashes on invalid input.

Summary

A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(b) can crash (segfault, invalid memory access) if A contains invalid (NaN/inf) entries. I would have expected garbage in/garbage out instead of garbage in/crash.

The same is even true for A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).rank(b)

It is true that A.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).info() would have returned InvalidInput (see the closed issue 2284), but given that not checking that will be common I still think that not crashing is desirable.

The reason for the possible crash is that the member variable m_nonzeroSingularValues of SVDBase will remain uninitialised on the early return of Jacobi SVD computation.

Environment

Observed in multiple environments.

Minimal Example

See problem description.

Steps to reproduce

See problem description.

What is the current bug behavior?

crash

What is the expected correct behavior?

No crash. Ideally some consistent output (possibly corresponding to rank=0), but I would totally agree with leaving it unspecified.

Possible Fix

Initialising m_nonzeroSingularValues to 0 in the constructor of SVDBase will solve the most common case. I can however imagine that its value could still become larger than the length of the diagonal if the object is reused, so setting it to zero in the early return of JacobiSVD an possibly other algorithms would also be good. That's the reason why I have not created a PR, I do not know the code well enough.