Skip to content

Commit

Permalink
BUG: Prevent integer underflow in CropImageFilter
Browse files Browse the repository at this point in the history
Check that the crop size is not greater that the image size to prevent
integer underflow.
  • Loading branch information
blowekamp committed Jan 10, 2020
1 parent 764737d commit 09e4a79
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Modules/Filtering/ImageGrid/include/itkCropImageFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ class ITK_TEMPLATE_EXPORT CropImageFilter : public ExtractImageFilter<TInputImag
void
GenerateOutputInformation() override;

void
VerifyInputInformation() ITKv5_CONST override;

private:
SizeType m_UpperBoundaryCropSize;
SizeType m_LowerBoundaryCropSize;
Expand Down
22 changes: 22 additions & 0 deletions Modules/Filtering/ImageGrid/include/itkCropImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ CropImageFilter<TInputImage, TOutputImage>::GenerateOutputInformation()
for (unsigned int i = 0; i < InputImageDimension; ++i)
{
idx[i] = input_idx[i] + m_LowerBoundaryCropSize[i];

assert(input_sz[i] >= (m_UpperBoundaryCropSize[i] + m_LowerBoundaryCropSize[i]));
sz[i] = input_sz[i] - (m_UpperBoundaryCropSize[i] + m_LowerBoundaryCropSize[i]);
}

Expand All @@ -56,6 +58,26 @@ CropImageFilter<TInputImage, TOutputImage>::GenerateOutputInformation()
Superclass::GenerateOutputInformation();
}


template <typename TInputImage, typename TOutputImage>
void
CropImageFilter<TInputImage, TOutputImage>::VerifyInputInformation() ITKv5_CONST
{
Superclass::VerifyInputInformation();

const TInputImage * inputPtr = this->GetInput();

InputImageSizeType input_sz = inputPtr->GetLargestPossibleRegion().GetSize();

for (unsigned int i = 0; i < InputImageDimension; ++i)
{
if (input_sz[i] < (m_UpperBoundaryCropSize[i] + m_LowerBoundaryCropSize[i]))
{
itkExceptionMacro("The input image's size " << input_sz << " is less than the total of the crop size!");
}
}
}

template <typename TInputImage, typename TOutputImage>
void
CropImageFilter<TInputImage, TOutputImage>::PrintSelf(std::ostream & os, Indent indent) const
Expand Down

0 comments on commit 09e4a79

Please sign in to comment.