From: David Wells Date: Sun, 11 Aug 2024 21:11:15 +0000 (-0600) Subject: PETScWrappers::VectorBase: add index conversion checks. X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1b99ded8bff2707c2da13498e1b0ef3bac82e678;p=dealii.git PETScWrappers::VectorBase: add index conversion checks. --- 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; }