]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Guard some calls to memcpy, memset and memmove to avoid undefined behavior 5471/head
authorDaniel Arndt <daniel.arndt@iwr.uni-heidelberg.de>
Wed, 15 Nov 2017 15:50:21 +0000 (16:50 +0100)
committerDaniel Arndt <d.arndt@math.uni-goettingen.de>
Wed, 15 Nov 2017 23:54:42 +0000 (00:54 +0100)
include/deal.II/base/partitioner.templates.h
source/dofs/dof_handler_policy.cc

index 4ffc40f4682fff0f302c864121924fec84e394af..398a8bb87457c3513d45145bb21a1c46442fe57e 100644 (file)
@@ -402,7 +402,11 @@ namespace Utilities
 
       // clear the ghost array in case we did not yet do that in the _start
       // function
-      std::memset(ghost_array.begin(), 0, sizeof(Number)*n_ghost_indices());
+      if (ghost_array.size()>0)
+        {
+          Assert(ghost_array.begin()!=nullptr, ExcInternalError());
+          std::memset(ghost_array.begin(), 0, sizeof(Number)*n_ghost_indices());
+        }
 
       // clear the compress requests
       requests.resize(0);
index e31a98ba592697da6c15e39a63589254ba67cf39..65636a49eeec3e0817e84d07f20f3527ccdaeb02 100644 (file)
@@ -3013,9 +3013,13 @@ namespace internal
             // know how to serialize itself. consequently, first copy it over
             // to an array of bytes, and then serialize that
             std::vector<char> quadrants_as_chars (sizeof(quadrants[0]) * quadrants.size());
-            std::memcpy(quadrants_as_chars.data(),
-                        quadrants.data(),
-                        quadrants_as_chars.size());
+            if (quadrants_as_chars.size()>0)
+              {
+                Assert(quadrants.data() != nullptr, ExcInternalError());
+                std::memcpy(quadrants_as_chars.data(),
+                            quadrants.data(),
+                            quadrants_as_chars.size());
+              }
 
             // now serialize everything
             ar &quadrants_as_chars
@@ -3037,10 +3041,15 @@ namespace internal
             &tree_indices
             &dof_numbers_and_indices;
 
-            quadrants.resize (quadrants_as_chars.size() / sizeof(quadrants[0]));
-            std::memcpy(quadrants.data(),
-                        quadrants_as_chars.data(),
-                        quadrants_as_chars.size());
+            if (quadrants_as_chars.size()>0)
+              {
+                quadrants.resize (quadrants_as_chars.size() / sizeof(quadrants[0]));
+                std::memcpy(quadrants.data(),
+                            quadrants_as_chars.data(),
+                            quadrants_as_chars.size());
+              }
+            else
+              quadrants.clear();
           }
 
           BOOST_SERIALIZATION_SPLIT_MEMBER()
@@ -4454,12 +4463,20 @@ namespace internal
                 break;
               n_ghosts_on_smaller_ranks += t.second;
             }
-          std::memmove(ghosted_new_numbers.data(),
-                       ghosted_new_numbers.data()+new_numbers.size(),
-                       sizeof(types::global_dof_index)*n_ghosts_on_smaller_ranks);
-          std::memcpy(ghosted_new_numbers.data()+n_ghosts_on_smaller_ranks,
-                      new_numbers.data(),
-                      sizeof(types::global_dof_index)*new_numbers.size());
+          if (n_ghosts_on_smaller_ranks>0)
+            {
+              Assert(ghosted_new_numbers.data()!=nullptr, ExcInternalError());
+              std::memmove(ghosted_new_numbers.data(),
+                           ghosted_new_numbers.data()+new_numbers.size(),
+                           sizeof(types::global_dof_index)*n_ghosts_on_smaller_ranks);
+            }
+          if (new_numbers.size()>0)
+            {
+              Assert (new_numbers.data()!=nullptr, ExcInternalError());
+              std::memcpy(ghosted_new_numbers.data()+n_ghosts_on_smaller_ranks,
+                          new_numbers.data(),
+                          sizeof(types::global_dof_index)*new_numbers.size());
+            }
         }
 
         // in case we do not own any of the given level (but only some remote

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.