From: David Wells Date: Sun, 11 Aug 2024 19:12:16 +0000 (-0600) Subject: PETScWrappers::MatrixBase::add(): assert indices are in range. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ec29de4f012a6ea6dd3cc52a4e365cedd0ecd84;p=dealii.git PETScWrappers::MatrixBase::add(): assert indices are in range. --- diff --git a/include/deal.II/lac/petsc_matrix_base.h b/include/deal.II/lac/petsc_matrix_base.h index 660d3aa682..599255ad29 100644 --- a/include/deal.II/lac/petsc_matrix_base.h +++ b/include/deal.II/lac/petsc_matrix_base.h @@ -1480,30 +1480,29 @@ namespace PETScWrappers prepare_action(VectorOperation::add); - const PetscInt petsc_i = row; - const PetscInt *col_index_ptr; - - const PetscScalar *col_value_ptr; - int n_columns; + const auto petsc_row = 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]; @@ -1516,13 +1515,15 @@ 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, n_columns, col_index_ptr, col_value_ptr, ADD_VALUES); + const PetscErrorCode ierr = MatSetValues(matrix, + 1, + &petsc_row, + n_columns, + column_indices.data(), + column_values.data(), + ADD_VALUES); AssertThrow(ierr == 0, ExcPETScError(ierr)); } @@ -1611,14 +1612,20 @@ namespace PETScWrappers inline bool MatrixBase::in_local_range(const size_type index) const { - PetscInt begin, end; + PetscInt petsc_begin, petsc_end; const PetscErrorCode ierr = - MatGetOwnershipRange(static_cast(matrix), &begin, &end); + MatGetOwnershipRange(static_cast(matrix), + &petsc_begin, + &petsc_end); AssertThrow(ierr == 0, ExcPETScError(ierr)); - return ((index >= static_cast(begin)) && - (index < static_cast(end))); + const auto begin = static_cast(petsc_begin); + AssertIntegerConversion(begin, petsc_begin); + const auto end = static_cast(petsc_end); + AssertIntegerConversion(end, petsc_end); + + return ((index >= begin) && (index < end)); }