From 75a6ebdb6629afc5032cac0a3397533b0404737c Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Mon, 22 Apr 2024 17:36:17 -0500 Subject: [PATCH] COMP: Avoid gcc13.2 compiler out-of-bounds index warnings 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. --- Modules/Core/Common/include/itkExtractImageFilter.hxx | 8 ++------ Modules/Core/Common/include/itkNeighborhood.h | 10 ++++++++++ Modules/Core/Common/include/itkNeighborhoodOperator.h | 5 +++++ .../test/itkShapedImageNeighborhoodRangeGTest.cxx | 8 +++++--- .../test/itkQuadEdgeMeshEulerOperatorSplitEdgeTest.cxx | 2 +- .../include/itkLabelMapContourOverlayImageFilter.hxx | 2 +- 6 files changed, 24 insertions(+), 11 deletions(-) diff --git a/Modules/Core/Common/include/itkExtractImageFilter.hxx b/Modules/Core/Common/include/itkExtractImageFilter.hxx index 430b7877d68e..1e236d376347 100644 --- a/Modules/Core/Common/include/itkExtractImageFilter.hxx +++ b/Modules/Core/Common/include/itkExtractImageFilter.hxx @@ -75,7 +75,7 @@ ExtractImageFilter::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]; @@ -116,11 +116,7 @@ ExtractImageFilter::GenerateOutputInformation() outputPtr->SetLargestPossibleRegion(m_OutputImageRegion); // Set the output spacing and origin - const ImageBase * phyData; - - phyData = dynamic_cast *>(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 diff --git a/Modules/Core/Common/include/itkNeighborhood.h b/Modules/Core/Common/include/itkNeighborhood.h index 511f50314cdb..ed1a16b277e8 100644 --- a/Modules/Core/Common/include/itkNeighborhood.h +++ b/Modules/Core/Common/include/itkNeighborhood.h @@ -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]; } @@ -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]; } diff --git a/Modules/Core/Common/include/itkNeighborhoodOperator.h b/Modules/Core/Common/include/itkNeighborhoodOperator.h index a95d39f3f7b2..55232f9e8454 100644 --- a/Modules/Core/Common/include/itkNeighborhoodOperator.h +++ b/Modules/Core/Common/include/itkNeighborhoodOperator.h @@ -93,6 +93,11 @@ class ITK_TEMPLATE_EXPORT NeighborhoodOperator : public Neighborhood= VDimension) + { + itkExceptionMacro(<< " Can not set direction " << direction << " greater than dimensionality of neighborhood " + << VDimension); + } m_Direction = direction; } diff --git a/Modules/Core/Common/test/itkShapedImageNeighborhoodRangeGTest.cxx b/Modules/Core/Common/test/itkShapedImageNeighborhoodRangeGTest.cxx index 8c39d45396bb..c44a7ddca8a7 100644 --- a/Modules/Core/Common/test/itkShapedImageNeighborhoodRangeGTest.cxx +++ b/Modules/Core/Common/test/itkShapedImageNeighborhoodRangeGTest.cxx @@ -914,9 +914,11 @@ TEST(ShapedImageNeighborhoodRange, ProvidesReverseIterators) const unsigned int numberOfNeighborhoodPixels = 3; const std::vector stdVector(range.begin(), range.end()); - std::vector reversedStdVector1(numberOfNeighborhoodPixels); - std::vector reversedStdVector2(numberOfNeighborhoodPixels); - std::vector reversedStdVector3(numberOfNeighborhoodPixels); + + assert(stdVector.size() == numberOfNeighborhoodPixels); + std::vector reversedStdVector1(numberOfNeighborhoodPixels); + std::vector reversedStdVector2(numberOfNeighborhoodPixels); + std::vector reversedStdVector3(numberOfNeighborhoodPixels); std::reverse_copy(stdVector.cbegin(), stdVector.cend(), reversedStdVector1.begin()); diff --git a/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshEulerOperatorSplitEdgeTest.cxx b/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshEulerOperatorSplitEdgeTest.cxx index c0111d0f0564..48043848d637 100644 --- a/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshEulerOperatorSplitEdgeTest.cxx +++ b/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshEulerOperatorSplitEdgeTest.cxx @@ -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; diff --git a/Modules/Filtering/ImageFusion/include/itkLabelMapContourOverlayImageFilter.hxx b/Modules/Filtering/ImageFusion/include/itkLabelMapContourOverlayImageFilter.hxx index 39f36cec512a..86a62abc34cf 100644 --- a/Modules/Filtering/ImageFusion/include/itkLabelMapContourOverlayImageFilter.hxx +++ b/Modules/Filtering/ImageFusion/include/itkLabelMapContourOverlayImageFilter.hxx @@ -157,7 +157,7 @@ LabelMapContourOverlayImageFilter::Befor srad.Fill(typename RadiusType::SizeValueType{}); for (unsigned int i = 0, j = 0; i < ImageDimension; ++i) { - if (j != static_cast(m_SliceDimension)) + if (j != static_cast(m_SliceDimension) and j < (ImageDimension - 1)) { srad[j] = m_ContourThickness[i]; ++j;