]> https://gitweb.dealii.org/ - dealii.git/commitdiff
CellIDTranslator:: add assert to prevent overflow 12513/head
authorPeter Munch <peterrmuench@gmail.com>
Mon, 28 Jun 2021 10:09:24 +0000 (12:09 +0200)
committerPeter Munch <peterrmuench@gmail.com>
Tue, 29 Jun 2021 14:23:51 +0000 (16:23 +0200)
include/deal.II/grid/cell_id_translator.h

index ae5b6d88030a3d5d592997e03bcbcc320da1bf5e..c857ee572452031a1a2cde4f1c31f8d0c9e5fcae 100644 (file)
@@ -21,6 +21,8 @@
 
 #include <deal.II/grid/cell_id.h>
 
+#include <cstdint>
+
 DEAL_II_NAMESPACE_OPEN
 
 namespace internal
@@ -119,6 +121,30 @@ namespace internal
     : n_coarse_cells(n_coarse_cells)
     , n_global_levels(n_global_levels)
   {
+    std::uint64_t max_cell_index = 0;
+
+    for (unsigned int i = 0; i < n_global_levels; ++i)
+      max_cell_index +=
+        Utilities::pow<std::uint64_t>(GeometryInfo<dim>::max_children_per_cell,
+                                      i) *
+        n_coarse_cells;
+
+    max_cell_index -= 1;
+
+    Assert(
+      max_cell_index <= std::numeric_limits<types::global_cell_index>::max(),
+      ExcMessage(
+        "You have exceeded the maximal number of possible indices this function "
+        "can handle. The current setup (n_coarse_cells=" +
+        std::to_string(n_coarse_cells) +
+        ", n_global_levels=" + std::to_string(n_global_levels) + ") requires " +
+        std::to_string(max_cell_index + 1) +
+        " indices but the current deal.II configuration only supports " +
+        std::to_string(std::numeric_limits<types::global_cell_index>::max()) +
+        " indices. You may want to consider to build deal.II with 64bit "
+        "indeces (-D DEAL_II_WITH_64BIT_INDICES=\"ON\") to increase the limit "
+        "of indices."));
+
     tree_sizes.push_back(0);
     for (unsigned int i = 0; i < n_global_levels; ++i)
       tree_sizes.push_back(

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.