Skip to content

Commit

Permalink
COMP: Avoid gcc13.2 compiler out-of-bounds index warnings
Browse files Browse the repository at this point in the history
The gcc 13.2 compiler warns about potential index out
of bounds errors in cases where the index is not explicitly
requested is not explicitly tested.

Avoid using index operator when index would be
out of known compile time bounds.
  • Loading branch information
hjmjohnson committed Apr 23, 2024
1 parent 3235b36 commit 75a6ebd
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
8 changes: 2 additions & 6 deletions Modules/Core/Common/include/itkExtractImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ ExtractImageFilter<TInputImage, TOutputImage>::SetExtractionRegion(InputImageReg
*/
for (unsigned int i = 0; i < InputImageDimension; ++i)
{
if (inputSize[i])
if (inputSize[i] and nonzeroSizeCount < OutputImageDimension)
{
outputSize[nonzeroSizeCount] = inputSize[i];
outputIndex[nonzeroSizeCount] = extractRegion.GetIndex()[i];
Expand Down Expand Up @@ -116,11 +116,7 @@ ExtractImageFilter<TInputImage, TOutputImage>::GenerateOutputInformation()
outputPtr->SetLargestPossibleRegion(m_OutputImageRegion);

// Set the output spacing and origin
const ImageBase<InputImageDimension> * phyData;

phyData = dynamic_cast<const ImageBase<InputImageDimension> *>(this->GetInput());

if (phyData)
if (this->GetInput())
{
// Copy what we can from the image from spacing and origin of the input
// This logic needs to be augmented with logic that select which
Expand Down
10 changes: 10 additions & 0 deletions Modules/Core/Common/include/itkNeighborhood.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ class ITK_TEMPLATE_EXPORT Neighborhood
SizeValueType
GetRadius(DimensionValueType n) const
{
if (n >= VDimension)
{
itkExceptionMacro(<< " Can not get radius for dimension " << n << " greater than dimensionality of neighborhood "
<< VDimension);
}
return m_Radius[n];
}

Expand All @@ -144,6 +149,11 @@ class ITK_TEMPLATE_EXPORT Neighborhood
SizeValueType
GetSize(DimensionValueType n) const
{
if (n >= VDimension)
{
itkExceptionMacro(<< " Can not get size for dimension " << n << " greater than dimensionality of neighborhood "
<< VDimension);
}
return m_Size[n];
}

Expand Down
5 changes: 5 additions & 0 deletions Modules/Core/Common/include/itkNeighborhoodOperator.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ class ITK_TEMPLATE_EXPORT NeighborhoodOperator : public Neighborhood<TPixel, VDi
void
SetDirection(const unsigned long direction)
{
if (direction >= VDimension)
{
itkExceptionMacro(<< " Can not set direction " << direction << " greater than dimensionality of neighborhood "
<< VDimension);
}
m_Direction = direction;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,11 @@ TEST(ShapedImageNeighborhoodRange, ProvidesReverseIterators)
const unsigned int numberOfNeighborhoodPixels = 3;

const std::vector<PixelType> stdVector(range.begin(), range.end());
std::vector<PixelType> reversedStdVector1(numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector2(numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector3(numberOfNeighborhoodPixels);

assert(stdVector.size() == numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector1(numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector2(numberOfNeighborhoodPixels);
std::vector<PixelType> reversedStdVector3(numberOfNeighborhoodPixels);

std::reverse_copy(stdVector.cbegin(), stdVector.cend(), reversedStdVector1.begin());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ itkQuadEdgeMeshEulerOperatorSplitEdgeTest(int, char *[])
auto splitEdge = SplitEdge::New();
std::cout << " "
<< "Test No Mesh Input";
if (splitEdge->Evaluate((QEType *)1))
if (splitEdge->Evaluate((QEType *){}))
{
std::cout << "FAILED." << std::endl;
return EXIT_FAILURE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ LabelMapContourOverlayImageFilter<TLabelMap, TFeatureImage, TOutputImage>::Befor
srad.Fill(typename RadiusType::SizeValueType{});
for (unsigned int i = 0, j = 0; i < ImageDimension; ++i)
{
if (j != static_cast<unsigned int>(m_SliceDimension))
if (j != static_cast<unsigned int>(m_SliceDimension) and j < (ImageDimension - 1))
{
srad[j] = m_ContourThickness[i];
++j;
Expand Down

0 comments on commit 75a6ebd

Please sign in to comment.