Repository navigation
BUG: Skip frequency iterator Init() for a default-constructed iterator - #7235
Conversation
Default-constructed frequency iterators, and those converted from a default-constructed ImageIteratorWithIndex, dereferenced a null m_Image in Init() and crashed. Return early when there is no image, matching the default constructors of the base iterators. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
There was a problem hiding this comment.
Thank you for contributing a pull request! 🙏
Welcome to the ITK community! 🤗👋☀️
We are glad you are here and appreciate your contribution. Please keep in mind our community participation guidelines. 📜
More support and guidance on the contribution process can be found in our contributing guide. 📖
This is an automatic message. Allow for time for the ITK community to be able to read the pull request and comment
on it.
[Low impact] This PR appears safe to merge.
|
hjmjohnson
left a comment
There was a problem hiding this comment.
Thanks, @edwardxuzhang1. The early return is the right fix: a default-constructed iterator is a supported state of the base class (ImageIteratorWithIndex() = default; with a null m_Image), every frequency member has a {} initializer, and the (image, region) constructor still dereferences the image before Init(), so the guard cannot hide a null image passed there.
Optional follow-up for the test (non-blocking)
If you'd like to strengthen DefaultConstructible in a follow-up PR:
- #7192 asks to convert a default-constructed
ImageIteratorWithIndexto each frequency iterator; the test converts toFrequencyImageRegionIteratorWithIndexonly. Adding the FFTLayout, HalfHermitian, and Shifted conversions exercises their guardedInit()too. - The test asserts nothing. A test that default-constructs an iterator, assigns a real one, and compares
GetFrequency()with a directly constructed iterator would check the case default construction exists for (reasoned from the code, not run):
TEST(FrequencyIteratorConversions, DefaultConstructedThenAssigned)
{
using ImageType = itk::Image<float, 2>;
auto image = ImageType::New();
image->SetRegions(ImageType::SizeType::Filled(5));
image->Allocate();
const auto region = image->GetLargestPossibleRegion();
itk::FrequencyFFTLayoutImageRegionIteratorWithIndex<ImageType> it{};
it = itk::ImageRegionIteratorWithIndex<ImageType>(image, region);
itk::FrequencyFFTLayoutImageRegionIteratorWithIndex<ImageType> expected(image, region);
for (; !expected.IsAtEnd(); ++it, ++expected)
{
EXPECT_EQ(it.GetFrequency(), expected.GetFrequency());
}
}
Default-constructed frequency iterators (and ones converted from a default-constructed
ImageIteratorWithIndex) dereference a nullm_ImageinInit()and segfault. Each of the fourInit()implementations now returns early when there is no image; a new GTest covers all eight writable/const iterator types plus the conversion.Fixes #7192
Testing and AI disclosure
ITKImageFrequencyGTestDriver --gtest_filter=FrequencyIteratorConversions.DefaultConstructiblesegfaults (exit 139) without the fix and passes with it;ctest -L ITKImageFrequencypasses 17/17. pre-commit hooks (clang-format, etc.) pass.Prepared with AI assistance; I reviewed and tested every changed line.
PR Checklist