From 3a0fc51a83248643c33266912034fb49927ae3ce Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Thu, 2 May 2024 14:49:33 -0500 Subject: [PATCH] COMP: Fix gcc 13.2 compiler warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Limit possible index overflow. In member function ‘SetVnlVector’, inlined from ‘_wrap_itkVectorUC2_SetVnlVector’ at ITK/cmake-build-release/Wrapping/Modules/ITKCommon/itkVectorPython.cpp:31005:0: ITK/Modules/Core/Common/include/itkVector.hxx:160: warning: writing 1 byte into a region of size 0 [-Wstringop-overflow=] 160 | (*this)[i] = v(i); | ITK/Modules/Core/Common/include/itkFixedArray.h: In function ‘_wrap_itkVectorUC2_SetVnlVector’: ITK/Modules/Core/Common/include/itkFixedArray.h:437:10: note: at offset 2 into destination object ‘m_InternalArray’ of size 2 437 | CArray m_InternalArray; | ^ --- Modules/Core/Common/include/itkVector.h | 3 ++- Modules/Core/Common/include/itkVector.hxx | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Modules/Core/Common/include/itkVector.h b/Modules/Core/Common/include/itkVector.h index 02fd8fec3284..8a5e1cdf8af8 100644 --- a/Modules/Core/Common/include/itkVector.h +++ b/Modules/Core/Common/include/itkVector.h @@ -90,7 +90,8 @@ class ITK_TEMPLATE_EXPORT Vector : public FixedArray return VVectorDimension; } - /** Set a vnl_vector_ref referencing the same memory block. */ + /** Copy values from the vnl_vector input to the internal memory block. The minimum of + * VVectorDimension and vnl_vector::size() elements are copied. */ void SetVnlVector(const vnl_vector &); diff --git a/Modules/Core/Common/include/itkVector.hxx b/Modules/Core/Common/include/itkVector.hxx index 408a9d8cb4f6..8d2fca35141b 100644 --- a/Modules/Core/Common/include/itkVector.hxx +++ b/Modules/Core/Common/include/itkVector.hxx @@ -155,7 +155,8 @@ template void Vector::SetVnlVector(const vnl_vector & v) { - for (unsigned int i = 0; i < v.size(); ++i) + const unsigned int elements_to_copy = std::min(TVectorDimension, v.size()); + for (unsigned int i = 0; i < elements_to_copy; ++i) { (*this)[i] = v(i); }