]> https://gitweb.dealii.org/ - dealii.git/commitdiff
PETScWrappers::VectorBase: add index conversion checks.
authorDavid Wells <drwells@email.unc.edu>
Sun, 11 Aug 2024 21:11:15 +0000 (15:11 -0600)
committerDavid Wells <drwells@email.unc.edu>
Mon, 12 Aug 2024 02:42:37 +0000 (20:42 -0600)
source/lac/petsc_vector_base.cc

index 17dd0557d61be88d9278ae7d9fc247d484185bac..69a999e49dca3c42e7f5bcd16f23fb38071f2754 100644 (file)
@@ -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<const PetscInt *>(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<PetscInt> petsc_indices(n_elements);
+    for (size_type i = 0; i < n_elements; ++i)
+      {
+        const auto petsc_index = static_cast<PetscInt>(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;
   }
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.