Fix inverse nullptr/asan errors for LU.

For empty or single-column matrices, the current `PartialPivLU`
currently dereferences a `nullptr` or accesses memory out-of-bounds.
Here we adjust the checks to avoid this.


(cherry picked from commit 154f00e9eacaec5667215784c7601b55024e2f61)
This commit is contained in:
Antonio Sanchez 2021-07-01 13:41:04 -07:00 committed by Rasmus Munk Larsen
parent 1f6b1c1a1f
commit b6db013435
2 changed files with 8 additions and 1 deletions

View File

@ -504,8 +504,13 @@ struct partial_lu_impl
template<typename MatrixType, typename TranspositionType>
void partial_lu_inplace(MatrixType& lu, TranspositionType& row_transpositions, typename TranspositionType::StorageIndex& nb_transpositions)
{
// Special-case of zero matrix.
if (lu.rows() == 0 || lu.cols() == 0) {
nb_transpositions = 0;
return;
}
eigen_assert(lu.cols() == row_transpositions.size());
eigen_assert((&row_transpositions.coeffRef(1)-&row_transpositions.coeffRef(0)) == 1);
eigen_assert(row_transpositions.size() < 2 || (&row_transpositions.coeffRef(1)-&row_transpositions.coeffRef(0)) == 1);
partial_lu_impl
< typename MatrixType::Scalar, MatrixType::Flags&RowMajorBit?RowMajor:ColMajor,

View File

@ -135,6 +135,8 @@ EIGEN_DECLARE_TEST(inverse)
CALL_SUBTEST_5( inverse(MatrixXf(s,s)) );
TEST_SET_BUT_UNUSED_VARIABLE(s)
CALL_SUBTEST_5( inverse_zerosized<float>() );
CALL_SUBTEST_5( inverse(MatrixXf(0, 0)) );
CALL_SUBTEST_5( inverse(MatrixXf(1, 1)) );
s = internal::random<int>(25,100);
CALL_SUBTEST_6( inverse(MatrixXcd(s,s)) );