InsightSoftwareConsortium / InsightSoftwareConsortium/ITK

Undefined behavior of iterators Get for VariableLengthVector

Open
#5,668 2 comments 0 reactions 0 assignees View on GitHub
type:Bug
Dominant language
C++
Stars
1.7k
Forks
748
Avg merge
1d 1h
Merged PRs (30d)
64

Description

### 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
#include

int
main(int argc, char * argv[])
{
using ImageType = itk::VectorImage;
auto img = ImageType::New();
img->SetVectorLength(3);
ImageType::SizeType size = { { 2, 2 } };
img->SetRegions(size);
img->AllocateInitialized();
itk::ImageRegionIterator 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 6a603279d6fd2c4a914a028e272a135c5bf45325.

### Environment

All OSs.

### Additional Information

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.