From f8cdda276ecf758d9f4c119c86838404b705d033 Mon Sep 17 00:00:00 2001
From: Martin Kronbichler <martin.kronbichler@it.uu.se>
Date: Fri, 5 Nov 2021 12:22:25 +0100
Subject: [PATCH] Fix some errors revealed by STL debug compile

---
 source/distributed/tria_base.cc | 12 ++++++++----
 source/grid/grid_generator.cc   | 20 ++++++++------------
 2 files changed, 16 insertions(+), 16 deletions(-)

diff --git a/source/distributed/tria_base.cc b/source/distributed/tria_base.cc
index 4cc72503b4..971288f4b1 100644
--- a/source/distributed/tria_base.cc
+++ b/source/distributed/tria_base.cc
@@ -1291,7 +1291,8 @@ namespace parallel
         // Adjust buffer iterator to the offset of the callback
         // function so that we only have to advance its position
         // to the next cell after each iteration.
-        dest_data_it = dest_data_fixed.cbegin() + offset;
+        if (cell_relations.begin() != cell_relations.end())
+          dest_data_it = dest_data_fixed.cbegin() + offset;
       }
 
     // Iterate over all cells and unpack the transferred data.
@@ -1334,8 +1335,10 @@ namespace parallel
                 data_increment -= offset;
               }
 
-            // Advance data size iterators to the next cell.
-            dest_sizes_cell_it += sizes_fixed_cumulative.back();
+            // Advance data size iterators to the next cell, avoid iterating
+            // past the end of dest_sizes_cell_it
+            if (cell_rel_it != cell_relations.end() - 1)
+              dest_sizes_cell_it += sizes_fixed_cumulative.back();
             ++dest_sizes_it;
           }
 
@@ -1369,7 +1372,8 @@ namespace parallel
               break;
           }
 
-        dest_data_it += data_increment;
+        if (cell_rel_it != cell_relations.end() - 1)
+          dest_data_it += data_increment;
       }
   }
 
diff --git a/source/grid/grid_generator.cc b/source/grid/grid_generator.cc
index f7d6f104a2..36f03a9226 100644
--- a/source/grid/grid_generator.cc
+++ b/source/grid/grid_generator.cc
@@ -4266,20 +4266,16 @@ namespace GridGenerator
     // compute cells to remove
     std::set<typename Triangulation<dim, spacedim>::active_cell_iterator>
       cells_to_remove;
-    std::copy_if(
-      rectangle.active_cell_iterators().begin(),
-      rectangle.active_cell_iterators().end(),
-      std::inserter(cells_to_remove, cells_to_remove.end()),
-      [&](
-        const typename Triangulation<dim, spacedim>::active_cell_iterator &cell)
-        -> bool {
-        for (unsigned int d = 0; d < dim; ++d)
+    for (const auto &cell : rectangle.active_cell_iterators())
+      {
+        bool remove_cell = true;
+        for (unsigned int d = 0; d < dim && remove_cell; ++d)
           if ((n_cells_to_remove[d] > 0 && cell->center()[d] >= cut_step[d]) ||
               (n_cells_to_remove[d] < 0 && cell->center()[d] <= cut_step[d]))
-            return false;
-
-        return true;
-      });
+            remove_cell = false;
+        if (remove_cell)
+          cells_to_remove.insert(cell);
+      }
 
     GridGenerator::create_triangulation_with_removed_cells(rectangle,
                                                            cells_to_remove,
-- 
2.39.5