From ce2e7f196fedca2d31685a40978bdf0111f4b436 Mon Sep 17 00:00:00 2001 From: David Wells Date: Sun, 11 Aug 2024 12:19:14 -0600 Subject: [PATCH] PETScWrappers::CommunicationPattern: assert indices are in range. --- source/lac/petsc_communication_pattern.cc | 25 ++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/source/lac/petsc_communication_pattern.cc b/source/lac/petsc_communication_pattern.cc index 3398e8b3a4..b8dcb27605 100644 --- a/source/lac/petsc_communication_pattern.cc +++ b/source/lac/petsc_communication_pattern.cc @@ -51,10 +51,17 @@ namespace PETScWrappers const MPI_Comm communicator) { clear(); + AssertIndexRange(local_size, ghost_indices.size() + 1); + // If the size of the index set can be converted to a PetscInt then every + // index can also be converted + AssertThrowIntegerConversion(ghost_indices.size(), + static_cast(ghost_indices.size())); PetscLayout layout; AssertPETSc(PetscLayoutCreate(communicator, &layout)); - AssertPETSc(PetscLayoutSetLocalSize(layout, local_size)); + const auto petsc_local_size = static_cast(local_size); + AssertThrowIntegerConversion(local_size, petsc_local_size); + AssertPETSc(PetscLayoutSetLocalSize(layout, petsc_local_size)); AssertPETSc(PetscLayoutSetUp(layout)); PetscInt start, end; @@ -88,6 +95,14 @@ namespace PETScWrappers const IndexSet &ghost_indices, const MPI_Comm communicator) { + // If the sizes of the index sets can be converted to PetscInts then every + // index can also be converted + AssertThrowIntegerConversion(static_cast( + locally_owned_indices.size()), + locally_owned_indices.size()); + AssertThrowIntegerConversion(static_cast(ghost_indices.size()), + ghost_indices.size()); + const auto in_deal = locally_owned_indices.get_index_vector(); std::vector in_petsc(in_deal.begin(), in_deal.end()); @@ -121,7 +136,9 @@ namespace PETScWrappers { if (i != numbers::invalid_dof_index) { - indices_has_clean.push_back(static_cast(i)); + const auto petsc_i = static_cast(i); + AssertThrowIntegerConversion(i, petsc_i); + indices_has_clean.push_back(petsc_i); indices_has_loc.push_back(loc); } else @@ -137,7 +154,9 @@ namespace PETScWrappers { if (i != numbers::invalid_dof_index) { - indices_want_clean.push_back(static_cast(i)); + const auto petsc_i = static_cast(i); + AssertThrowIntegerConversion(i, petsc_i); + indices_want_clean.push_back(petsc_i); indices_want_loc.push_back(loc); } else -- 2.39.5