From 1b99ded8bff2707c2da13498e1b0ef3bac82e678 Mon Sep 17 00:00:00 2001 From: David Wells Date: Sun, 11 Aug 2024 15:11:15 -0600 Subject: [PATCH] PETScWrappers::VectorBase: add index conversion checks. --- source/lac/petsc_vector_base.cc | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/source/lac/petsc_vector_base.cc b/source/lac/petsc_vector_base.cc index 17dd0557d6..69a999e49d 100644 --- a/source/lac/petsc_vector_base.cc +++ b/source/lac/petsc_vector_base.cc @@ -1078,25 +1078,20 @@ namespace PETScWrappers Assert((last_action == action) || (last_action == VectorOperation::unknown), internal::VectorReference::ExcWrongMode(action, last_action)); Assert(!has_ghost_elements(), ExcGhostsPresent()); - // VecSetValues complains if we - // come with an empty - // vector. however, it is not a - // collective operation, so we - // can skip the call if necessary - // (unlike the above calls) - if (n_elements != 0) - { - const PetscInt *petsc_indices = - reinterpret_cast(indices); - const InsertMode mode = (add_values ? ADD_VALUES : INSERT_VALUES); - const PetscErrorCode ierr = - VecSetValues(vector, n_elements, petsc_indices, values, mode); - AssertThrow(ierr == 0, ExcPETScError(ierr)); + std::vector petsc_indices(n_elements); + for (size_type i = 0; i < n_elements; ++i) + { + const auto petsc_index = static_cast(indices[i]); + AssertIntegerConversion(petsc_index, indices[i]); + petsc_indices[i] = petsc_index; } - // set the mode here, independent of whether we have actually - // written elements or whether the list was empty + const InsertMode mode = (add_values ? ADD_VALUES : INSERT_VALUES); + const PetscErrorCode ierr = VecSetValues( + vector, petsc_indices.size(), petsc_indices.data(), values, mode); + AssertThrow(ierr == 0, ExcPETScError(ierr)); + last_action = action; } -- 2.39.5