From 7d152c7e59afedddaace5b0eadda463c1541096c Mon Sep 17 00:00:00 2001 From: David Wells Date: Sun, 11 Aug 2024 13:13:35 -0600 Subject: [PATCH] IndexSet::make_petsc_is(): assert indices are in range. --- source/base/index_set.cc | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/source/base/index_set.cc b/source/base/index_set.cc index 35d91f9217..66885684ec 100644 --- a/source/base/index_set.cc +++ b/source/base/index_set.cc @@ -1112,12 +1112,20 @@ IndexSet::make_petsc_is(const MPI_Comm communicator) const std::vector indices; fill_index_vector(indices); - PetscInt n = indices.size(); - std::vector pindices(indices.begin(), indices.end()); + // If the size of the index set can be converted to a PetscInt then every + // value can also be converted + AssertThrowIntegerConversion(static_cast(size()), size()); + const auto local_size = static_cast(n_elements()); + AssertIntegerConversion(local_size, n_elements()); + + size_type i = 0; + std::vector petsc_indices(n_elements()); + for (const auto &index : *this) + petsc_indices[i] = static_cast(index); IS is; - PetscErrorCode ierr = - ISCreateGeneral(communicator, n, pindices.data(), PETSC_COPY_VALUES, &is); + PetscErrorCode ierr = ISCreateGeneral( + communicator, local_size, petsc_indices.data(), PETSC_COPY_VALUES, &is); AssertThrow(ierr == 0, ExcPETScError(ierr)); return is; -- 2.39.5