From 26eeb909bfbd73aa352103a054a409a93334ba23 Mon Sep 17 00:00:00 2001 From: Daniel Garcia-Sanchez Date: Mon, 26 Sep 2022 19:16:02 +0200 Subject: [PATCH] Do not dereference cend Avoid deferencing cend because it is undefined behaviour. https://en.cppreference.com/w/cpp/container/vector/end --- include/deal.II/base/utilities.h | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index dab4066dad..519add5db3 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -1252,7 +1252,10 @@ namespace Utilities const std::vector::const_iterator &cend, std::vector & 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::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(&*cbegin + - sizeof(vector_size)), - reinterpret_cast(&*cend)); + { + const auto buffer_data_begin = + reinterpret_cast(&*cbegin + sizeof(vector_size)); + const auto buffer_data_end = buffer_data_begin + vector_size; + object.insert(object.end(), buffer_data_begin, buffer_data_end); + } } -- 2.39.5