Skip to content

BUG: Skip frequency iterator Init() for a default-constructed iterator - #7235

Merged
hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
edwardxuzhang1:frequency-iterator-null-image
Nov 25, 2026
Merged

hjmjohnson merged 1 commit into
InsightSoftwareConsortium:mainfrom
edwardxuzhang1:frequency-iterator-null-image

Conversation

@edwardxuzhang1

Copy link
Copy Markdown
Contributor

Default-constructed frequency iterators (and ones converted from a default-constructed ImageIteratorWithIndex) dereference a null m_Image in Init() and segfault. Each of the four Init() 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.DefaultConstructible segfaults (exit 139) without the fix and passes with it; ctest -L ITKImageFrequency passes 17/17. pre-commit hooks (clang-format, etc.) pass.

Prepared with AI assistance; I reviewed and tested every changed line.

PR Checklist

  • No API changes were made (or the changes have been approved)
  • No major design changes were made (or the changes have been approved)
  • Added test (or behavior not changed)
  • Updated API documentation (or API not changed)
  • Added license to new files (if any)
  • Added Python wrapping to new files (if any) as described in ITK Software Guide Section 9.5
  • Added ITK examples for all new major features (if any)

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>
@github-actions github-actions Bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct area:Filtering Issues affecting the Filtering module labels Nov 25, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dzenanz
dzenanz marked this pull request as ready for review November 25, 2026 17:08
@greptile-apps

greptile-apps Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Low impact] This PR appears safe to merge.

Summary

Prevents default-constructed frequency iterators from reading a null image.

  • Frequency iterators can be constructed without an attached image.

Reviews (1) · Last reviewed commit: "BUG: Skip frequency iterator Init() for ..." · Reviewed by Greptile

@dzenanz
dzenanz requested a review from hjmjohnson November 25, 2026 17:25

@hjmjohnson hjmjohnson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ImageIteratorWithIndex to each frequency iterator; the test converts to FrequencyImageRegionIteratorWithIndex only. Adding the FFTLayout, HalfHermitian, and Shifted conversions exercises their guarded Init() 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());
  }
}

@hjmjohnson
hjmjohnson merged commit 8143043 into InsightSoftwareConsortium:main Nov 25, 2026
20 checks passed
@hjmjohnson hjmjohnson added the status:Confirmed Confirmed/reproduced issue on a different machine with same or similar settings to those reported label Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Filtering Issues affecting the Filtering module status:Confirmed Confirmed/reproduced issue on a different machine with same or similar settings to those reported type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances type:Testing Ensure that the purpose of a class is met/the results on a wide set of test cases are correct

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Default-constructed frequency image iterators crash (Init() dereferences a null image)

3 participants