From: Daniel Garcia-Sanchez Date: Mon, 26 Sep 2022 17:16:02 +0000 (+0200) Subject: Do not dereference cend X-Git-Tag: v9.5.0-rc1~922^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=26eeb909bfbd73aa352103a054a409a93334ba23;p=dealii.git Do not dereference cend Avoid deferencing cend because it is undefined behaviour. https://en.cppreference.com/w/cpp/container/vector/end --- 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); + } }