Skip to content

Commit

Permalink
COMP: Fix gcc 13.2 compiler warnings
Browse files Browse the repository at this point in the history
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;
      |          ^
  • Loading branch information
hjmjohnson committed May 8, 2024
1 parent 6dcfb74 commit 408cf0c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Modules/Core/Common/include/itkVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class ITK_TEMPLATE_EXPORT Vector : public FixedArray<T, VVectorDimension>
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<T> &);

Expand Down
3 changes: 2 additions & 1 deletion Modules/Core/Common/include/itkVector.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ template <typename T, unsigned int TVectorDimension>
void
Vector<T, TVectorDimension>::SetVnlVector(const vnl_vector<T> & v)
{
for (unsigned int i = 0; i < v.size(); ++i)
const unsigned int elements_to_copy = std::min<unsigned int>(TVectorDimension, v.size());
for (unsigned int i = 0; i < elements_to_copy; ++i)
{
(*this)[i] = v(i);
}
Expand Down

0 comments on commit 408cf0c

Please sign in to comment.