Skip to content

ImageMaskSpatialObject tries to access pixels outside the image buffer (segfault) #1950

@N-Dekker

Description

@N-Dekker

Description

ImageMaskSpatialObject::IsInsideInObjectSpace does in some border cases try to access pixels outside the image buffer, which sometimes triggers a segmentation fault, or an incorrect result. This issue appears related to some specific elastix 5.0.0 segfaults reported by Theo van Walsum (@tvanwalsum, Erasmus MC, Rotterdam) at Google Groups - elastix-imageregistration - Issues with mask in el 5.0.0 when mask extent is smaller then image extent.

Steps to Reproduce Assert Failure

#include <itkImage.h>
#include <itkSize.h>
#include <itkImageMaskSpatialObject.h>
#include <cassert>
#include <cstdlib>

int main()
{
	const auto image = itk::Image<unsigned char>::New();
	image->SetRegions(itk::Size<>{ {2, 2} });
	image->Allocate(true);
	const auto imageMaskSpatialObject = itk::ImageMaskSpatialObject<2>::New();
	imageMaskSpatialObject->SetImage(image);
	const double point[] = { 1.5, 1.5 };
	if (imageMaskSpatialObject->IsInsideInObjectSpace(point))
	{
		assert(!"Incorrectly estimated that the specified point is inside the mask!");
		return EXIT_FAILURE;
	}
	return EXIT_SUCCESS;
}

Expected behavior

return EXIT_SUCCESS.

Actual behavior

assert failure, return EXIT_FAILURE.

Steps to Reproduce Segfault

#include <itkImage.h>
#include <itkSize.h>
#include <itkImageMaskSpatialObject.h>

int main()
{
	const auto image = itk::Image<unsigned char, 3>::New();
	const itk::Size<3> imageSize{ {256, 128, 32} };
	image->SetRegions(imageSize);
	const double origin[] = { 0.0, -16.0, 0.0 };
	image->SetOrigin(origin);
	image->Allocate(true);

	const auto imageMaskSpatialObject = itk::ImageMaskSpatialObject<>::New();
	imageMaskSpatialObject->SetImage(image);
	const double point[] = { 0.0, 0.0, 31.5 };
	imageMaskSpatialObject->IsInsideInObjectSpace(point); // read access violation!
}

Reproducibility

Segmentation faults only seem to happen for specific combinations of image size, origin, and point coordinates.

Versions

Reproduced with ITK v5.0.1, as well as the current version (August 6, 2020) at the master: a1a7e0b

Environment

Visual Studio 2019 (Debug). Note that the issue does not seem to be specific to Visual Studio.

Additional Information

There is a possible fix at pull request #1951

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:BugInconsistencies or issues which will cause an incorrect result under some or all circumstances

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions