From: Wolfgang Bangerth <bangerth@colostate.edu>
Date: Mon, 12 Jun 2023 16:57:12 +0000 (-0600)
Subject: Make sure a conversion is safe.
X-Git-Tag: v9.5.0-rc1~108^2
X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4e27e9bf747d1009323731cf51b058eb8a641425;p=dealii.git

Make sure a conversion is safe.
---

diff --git a/source/lac/petsc_vector_base.cc b/source/lac/petsc_vector_base.cc
index a3280a8b24..7b762d875a 100644
--- a/source/lac/petsc_vector_base.cc
+++ b/source/lac/petsc_vector_base.cc
@@ -259,8 +259,20 @@ namespace PETScWrappers
         AssertThrow(ierr == 0, ExcPETScError(ierr));
         ierr = VecGetArray(tvector, &array);
         AssertThrow(ierr == 0, ExcPETScError(ierr));
+
+        // Store the indices we care about in the vector, so that we can then
+        // exchange this information between processes. It is unfortunate that
+        // we have to store integers in floating point numbers. Let's at least
+        // make sure we do that in a way that ensures that when we get these
+        // numbers back as integers later on, we get the same thing.
         for (PetscInt i = 0; i < end_index - ghost_start_index; i++)
-          array[i] = ghost_start_index + i;
+          {
+            Assert(static_cast<PetscInt>(static_cast<PetscScalar>(
+                     ghost_start_index + i)) == (ghost_start_index + i),
+                   ExcInternalError());
+            array[i] = ghost_start_index + i;
+          }
+
         ierr = VecRestoreArray(tvector, &array);
         AssertThrow(ierr == 0, ExcPETScError(ierr));
         ierr = VecGhostUpdateBegin(tvector, INSERT_VALUES, SCATTER_FORWARD);