Skip to content

Undefined behavior of iterators Get for VariableLengthVector #5668

@SimonRit

Description

@SimonRit

Description

The Get function of an iterator on an itk::Image of itk::VariableLengthVector returns an itk::VariableLengthVector which data points to a pixel of the itk::Image. This is because the DefaultVectorPixelAccessor::Get calls the VariableLengthVector constructor parameterized by a const pointer. This leads to an unexpected behavior for the user.

Steps to Reproduce

This is a minimal example reproducing the issue:

#include <itkVectorImage.h>
#include <itkImageRegionIterator.h>

int
main(int argc, char * argv[])
{
  using ImageType = itk::VectorImage<float,2>;
  auto img = ImageType::New();
  img->SetVectorLength(3);
  ImageType::SizeType size = { { 2, 2 } };
  img->SetRegions(size);
  img->AllocateInitialized();
  itk::ImageRegionIterator<ImageType> it(img, img->GetLargestPossibleRegion());
  auto v { it.Get() };
  float c = 0.;
  while(!it.IsAtEnd())
  {
    v.Fill(c++);
    it.Set(v);
    ++it;
  }
  it.GoToBegin();
  while(!it.IsAtEnd())
  {
    std::cout << it.Get() << std::endl;
    ++it;
  }
  return EXIT_SUCCESS;
}

Expected behavior

It should display

[0, 0, 0]
[1, 1, 1]
[2, 2, 2]
[3, 3, 3]

Actual behavior

It actually displays

[3, 3, 3]
[1, 1, 1]
[2, 2, 2]
[3, 3, 3]

Reproducibility

100%

Versions

All versions since 6a60327.

Environment

All OSs.

Additional Information

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

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions