]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Do not dereference cend 14313/head
authorDaniel Garcia-Sanchez <daniel.garcia-sanchez@insp.upmc.fr>
Mon, 26 Sep 2022 17:16:02 +0000 (19:16 +0200)
committerDaniel Garcia-Sanchez <daniel.garcia-sanchez@insp.upmc.fr>
Mon, 26 Sep 2022 17:16:02 +0000 (19:16 +0200)
Avoid deferencing cend because it is undefined behaviour.
https://en.cppreference.com/w/cpp/container/vector/end

include/deal.II/base/utilities.h

index dab4066dadb26baf7b8acf418bba8fd65162532c..519add5db3f89a2ab18d14db3b1ee6ea2afaa488 100644 (file)
@@ -1252,7 +1252,10 @@ namespace Utilities
       const std::vector<char>::const_iterator &cend,
       std::vector<T> &                         object)
     {
-      // First get the size of the vector, and resize the output object
+      // The size of the object vector can be found in cbegin of the buffer.
+      // The data starts at cbegin + sizeof(vector_size).
+
+      // Get the size of the vector
       typename std::vector<T>::size_type vector_size;
       memcpy(&vector_size, &*cbegin, sizeof(vector_size));
 
@@ -1262,13 +1265,15 @@ namespace Utilities
              ExcMessage("The given buffer has the wrong size."));
       (void)cend;
 
-      // Then copy the elements:
+      // Copy the elements:
       object.clear();
       if (vector_size > 0)
-        object.insert(object.end(),
-                      reinterpret_cast<const T *>(&*cbegin +
-                                                  sizeof(vector_size)),
-                      reinterpret_cast<const T *>(&*cend));
+        {
+          const auto buffer_data_begin =
+            reinterpret_cast<const T *>(&*cbegin + sizeof(vector_size));
+          const auto buffer_data_end = buffer_data_begin + vector_size;
+          object.insert(object.end(), buffer_data_begin, buffer_data_end);
+        }
     }
 
 

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.