Skip to content

Commit

Permalink
Check for integer overflow when copying sizes from SuiteSparse matrices.
Browse files Browse the repository at this point in the history
* liboctave/array/CSparse.cc, dSparse.cc (fsolve),
liboctave/numeric/sparse-chol.cc (sparse_chol_rep::P),
liboctave/numeric/sparse-qr.cc (sparse_qr<SparseMatrix>::sparse_qr_rep::R): Use
"from_size_t" instead of casting nrow and ncol from SuiteSparse matrix
structures without any overflow check.
  • Loading branch information
mmuetzel committed Sep 8, 2023
1 parent ebad964 commit 574afd3
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
8 changes: 4 additions & 4 deletions liboctave/array/CSparse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6040,8 +6040,8 @@ SparseComplexMatrix::fsolve (MatrixType& mattype, const SparseMatrix& b,
cholmod_sparse *X = CHOLMOD_NAME(spsolve) (CHOLMOD_A, L, B, cm);

retval = SparseComplexMatrix
(static_cast<octave_idx_type> (X->nrow),
static_cast<octave_idx_type> (X->ncol),
(octave::from_size_t (X->nrow),
octave::from_size_t (X->ncol),
octave::from_suitesparse_long (CHOLMOD_NAME(nnz)
(X, cm)));
for (octave_idx_type j = 0;
Expand Down Expand Up @@ -6550,8 +6550,8 @@ SparseComplexMatrix::fsolve (MatrixType& mattype, const SparseComplexMatrix& b,
cholmod_sparse *X = CHOLMOD_NAME(spsolve) (CHOLMOD_A, L, B, cm);

retval = SparseComplexMatrix
(static_cast<octave_idx_type> (X->nrow),
static_cast<octave_idx_type> (X->ncol),
(octave::from_size_t (X->nrow),
octave::from_size_t (X->ncol),
octave::from_suitesparse_long (CHOLMOD_NAME(nnz)
(X, cm)));
for (octave_idx_type j = 0;
Expand Down
8 changes: 4 additions & 4 deletions liboctave/array/dSparse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6054,8 +6054,8 @@ SparseMatrix::fsolve (MatrixType& mattype, const SparseMatrix& b,
cholmod_sparse *X = CHOLMOD_NAME(spsolve) (CHOLMOD_A, L, B, cm);

retval = SparseMatrix
(static_cast<octave_idx_type> (X->nrow),
static_cast<octave_idx_type> (X->ncol),
(octave::from_size_t (X->nrow),
octave::from_size_t (X->ncol),
octave::from_suitesparse_long (CHOLMOD_NAME(nnz)
(X, cm)));
for (octave_idx_type j = 0;
Expand Down Expand Up @@ -6549,8 +6549,8 @@ SparseMatrix::fsolve (MatrixType& mattype, const SparseComplexMatrix& b,
cholmod_sparse *X = CHOLMOD_NAME(spsolve) (CHOLMOD_A, L, B, cm);

retval = SparseComplexMatrix
(static_cast<octave_idx_type> (X->nrow),
static_cast<octave_idx_type> (X->ncol),
(octave::from_size_t (X->nrow),
octave::from_size_t (X->ncol),
octave::from_suitesparse_long (CHOLMOD_NAME(nnz)
(X, cm)));
for (octave_idx_type j = 0;
Expand Down
3 changes: 1 addition & 2 deletions liboctave/numeric/sparse-chol.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ class sparse_chol<chol_type>::sparse_chol_rep
octave_idx_type P () const
{
#if defined (HAVE_CHOLMOD)
return (m_minor_p == static_cast<octave_idx_type> (m_L->ncol) ?
0 : m_minor_p + 1);
return (m_minor_p == from_size_t (m_L->ncol) ? 0 : m_minor_p + 1);
#else
return 0;
#endif
Expand Down
4 changes: 2 additions & 2 deletions liboctave/numeric/sparse-qr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -634,8 +634,8 @@ sparse_qr<SparseMatrix>::sparse_qr_rep::R (bool econ) const
{
#if (defined (HAVE_SPQR) && defined (HAVE_CHOLMOD))

octave_idx_type nr = static_cast<octave_idx_type> (m_R->nrow);
octave_idx_type nc = static_cast<octave_idx_type> (m_R->ncol);
octave_idx_type nr = from_size_t (m_R->nrow);
octave_idx_type nc = from_size_t (m_R->ncol);
octave_idx_type nz
= from_suitesparse_long
(cholmod_l_nnz (m_R, const_cast<cholmod_common *> (&m_cc)));
Expand Down

0 comments on commit 574afd3

Please sign in to comment.