]> https://gitweb.dealii.org/ - dealii.git/commitdiff
PETScWrappers::MatrixBase::set(): assert indices are in range.
authorDavid Wells <drwells@email.unc.edu>
Sun, 11 Aug 2024 18:49:08 +0000 (12:49 -0600)
committerDavid Wells <drwells@email.unc.edu>
Mon, 12 Aug 2024 02:42:37 +0000 (20:42 -0600)
include/deal.II/lac/petsc_matrix_base.h

index 66faeee3ce446781b46c117658253de29c85eba0..660d3aa682054285555561a1c65df57228c36c38 100644 (file)
@@ -1340,30 +1340,29 @@ namespace PETScWrappers
   {
     prepare_action(VectorOperation::insert);
 
-    const PetscInt  petsc_i = row;
-    const PetscInt *col_index_ptr;
-
-    const PetscScalar *col_value_ptr;
-    int                n_columns;
+    const auto petsc_row = static_cast<PetscInt>(row);
+    AssertIntegerConversion(petsc_row, row);
 
-    // If we don't elide zeros, the pointers are already available...
+    PetscInt n_columns = 0;
+    column_indices.resize(n_cols);
+    column_values.resize(n_cols);
     if (elide_zero_values == false)
       {
-        col_index_ptr = reinterpret_cast<const PetscInt *>(col_indices);
-        col_value_ptr = values;
-        n_columns     = n_cols;
+        n_columns = static_cast<PetscInt>(n_cols);
+        AssertIntegerConversion(n_columns, n_cols);
+
+        for (size_type j = 0; j < n_cols; ++j)
+          {
+            AssertIsFinite(values[j]);
+            column_indices[j] = static_cast<PetscInt>(col_indices[j]);
+            AssertIntegerConversion(column_indices[j], col_indices[j]);
+            column_values[j] = values[j];
+          }
       }
     else
       {
         // Otherwise, extract nonzero values in each row and get the
         // respective index.
-        if (column_indices.size() < n_cols)
-          {
-            column_indices.resize(n_cols);
-            column_values.resize(n_cols);
-          }
-
-        n_columns = 0;
         for (size_type j = 0; j < n_cols; ++j)
           {
             const PetscScalar value = values[j];
@@ -1376,17 +1375,14 @@ namespace PETScWrappers
               }
           }
         AssertIndexRange(n_columns, n_cols + 1);
-
-        col_index_ptr = column_indices.data();
-        col_value_ptr = column_values.data();
       }
 
     const PetscErrorCode ierr = MatSetValues(matrix,
                                              1,
-                                             &petsc_i,
+                                             &petsc_row,
                                              n_columns,
-                                             col_index_ptr,
-                                             col_value_ptr,
+                                             column_indices.data(),
+                                             column_values.data(),
                                              INSERT_VALUES);
     AssertThrow(ierr == 0, ExcPETScError(ierr));
   }

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.