From 60dbd627ed9a35ecb9c01982167506e8385ff427 Mon Sep 17 00:00:00 2001 From: David Wells Date: Sun, 11 Aug 2024 12:49:08 -0600 Subject: [PATCH] PETScWrappers::MatrixBase::set(): assert indices are in range. --- include/deal.II/lac/petsc_matrix_base.h | 40 +++++++++++-------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/include/deal.II/lac/petsc_matrix_base.h b/include/deal.II/lac/petsc_matrix_base.h index 66faeee3ce..660d3aa682 100644 --- a/include/deal.II/lac/petsc_matrix_base.h +++ b/include/deal.II/lac/petsc_matrix_base.h @@ -1340,30 +1340,29 @@ namespace PETScWrappers { prepare_action(VectorOperation::insert); - const PetscInt petsc_i = row; - const PetscInt *col_index_ptr; - - const PetscScalar *col_value_ptr; - int n_columns; + const auto petsc_row = static_cast(row); + AssertIntegerConversion(petsc_row, row); - // If we don't elide zeros, the pointers are already available... + PetscInt n_columns = 0; + column_indices.resize(n_cols); + column_values.resize(n_cols); if (elide_zero_values == false) { - col_index_ptr = reinterpret_cast(col_indices); - col_value_ptr = values; - n_columns = n_cols; + n_columns = static_cast(n_cols); + AssertIntegerConversion(n_columns, n_cols); + + for (size_type j = 0; j < n_cols; ++j) + { + AssertIsFinite(values[j]); + column_indices[j] = static_cast(col_indices[j]); + AssertIntegerConversion(column_indices[j], col_indices[j]); + column_values[j] = values[j]; + } } else { // Otherwise, extract nonzero values in each row and get the // respective index. - if (column_indices.size() < n_cols) - { - column_indices.resize(n_cols); - column_values.resize(n_cols); - } - - n_columns = 0; for (size_type j = 0; j < n_cols; ++j) { const PetscScalar value = values[j]; @@ -1376,17 +1375,14 @@ namespace PETScWrappers } } AssertIndexRange(n_columns, n_cols + 1); - - col_index_ptr = column_indices.data(); - col_value_ptr = column_values.data(); } const PetscErrorCode ierr = MatSetValues(matrix, 1, - &petsc_i, + &petsc_row, n_columns, - col_index_ptr, - col_value_ptr, + column_indices.data(), + column_values.data(), INSERT_VALUES); AssertThrow(ierr == 0, ExcPETScError(ierr)); } -- 2.39.5