From d8c8ee6fb2cfe1af53b020ff10a70fbd9b47ccad Mon Sep 17 00:00:00 2001 From: Tyler Veness Date: Thu, 5 Mar 2026 17:43:21 -0800 Subject: [PATCH] Fix crash on construction of SparseMatrix with zero-length diagonal libeigen/eigen!2249 --- Eigen/src/SparseCore/SparseMatrix.h | 2 +- test/sparse_basic.cpp | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Eigen/src/SparseCore/SparseMatrix.h b/Eigen/src/SparseCore/SparseMatrix.h index 782e59511..36646c8ab 100644 --- a/Eigen/src/SparseCore/SparseMatrix.h +++ b/Eigen/src/SparseCore/SparseMatrix.h @@ -1008,7 +1008,7 @@ class SparseMatrix : public SparseCompressedBase>::value; if (overwrite) { - if ((m_outerSize != n) || (m_innerSize != n)) resize(n, n); + if ((m_outerSize != n) || (m_innerSize != n) || (n == 0)) resize(n, n); } if (m_data.size() == 0 || overwrite) { diff --git a/test/sparse_basic.cpp b/test/sparse_basic.cpp index b239eab58..f1fd6b771 100644 --- a/test/sparse_basic.cpp +++ b/test/sparse_basic.cpp @@ -871,6 +871,10 @@ void sparse_basic(const SparseMatrixType& ref) { m2 -= d.asDiagonal(); refMat2 -= d.asDiagonal(); VERIFY_IS_APPROX(m2, refMat2); + + // zero-length diagonal + d = DenseVector(0); + SparseMatrixType m4(d.asDiagonal()); } // test conservative resize