Fix segfault in CholmodBase::factorize() for zero matrix

This commit is contained in:
Tyler Veness 2024-02-12 03:27:56 +00:00 committed by Charles Schlosser
parent 7b87b21910
commit d1d87973f4
3 changed files with 20 additions and 2 deletions

View File

@ -329,8 +329,10 @@ class CholmodBase : public SparseSolverBase<Derived> {
cholmod_sparse A = viewAsCholmod(matrix.template selfadjointView<UpLo>());
internal::cm_factorize_p<StorageIndex>(&A, m_shiftOffset, 0, 0, m_cholmodFactor, m_cholmod);
// If the factorization failed, minor is the column at which it did. On success minor == n.
this->m_info = (m_cholmodFactor->minor == m_cholmodFactor->n ? Success : NumericalIssue);
// If the factorization failed, either the input matrix was zero (so m_cholmodFactor == nullptr), or minor is the
// column at which it failed. On success minor == n.
this->m_info =
(m_cholmodFactor != nullptr && m_cholmodFactor->minor == m_cholmodFactor->n ? Success : NumericalIssue);
m_factorizationIsOk = true;
}

View File

@ -54,6 +54,13 @@ void test_cholmod_ST() {
check_sparse_spd_determinant(llt_colmajor_upper);
check_sparse_spd_determinant(ldlt_colmajor_lower);
check_sparse_spd_determinant(ldlt_colmajor_upper);
check_sparse_zero_matrix(chol_colmajor_lower);
check_sparse_zero_matrix(chol_colmajor_upper);
check_sparse_zero_matrix(llt_colmajor_lower);
check_sparse_zero_matrix(llt_colmajor_upper);
check_sparse_zero_matrix(ldlt_colmajor_lower);
check_sparse_zero_matrix(ldlt_colmajor_upper);
}
template <typename T, int flags, typename IdxType>

View File

@ -484,6 +484,15 @@ void check_sparse_spd_determinant(Solver& solver) {
}
}
template <typename Solver>
void check_sparse_zero_matrix(Solver& solver) {
typedef typename Solver::MatrixType Mat;
Mat A(1, 1);
solver.compute(A);
VERIFY_IS_EQUAL(solver.info(), NumericalIssue);
}
template <typename Solver, typename DenseMat>
Index generate_sparse_square_problem(Solver&, typename Solver::MatrixType& A, DenseMat& dA, int maxSize = 300,
int options = ForceNonZeroDiag) {