Skip to content

BUG: Fix possible undefined behavior in Transform::ApplyToImageMetadata - #6914

Merged
blowekamp merged 1 commit into
InsightSoftwareConsortium:release-5.4from
blowekamp:backport-6583-r54
Sep 29, 2026
Merged

blowekamp merged 1 commit into
InsightSoftwareConsortium:release-5.4from
blowekamp:backport-6583-r54

Conversation

@blowekamp

Copy link
Copy Markdown
Member

Backport of #6583 to release-5.4.

itk::Transform::ApplyToImageMetadata() calls this->GetInverseTransform()
and dereferences the result without checking for null. For a non-invertible
transform (e.g. a singular AffineTransform), GetInverseTransform()
returns a null Pointer, so this is an unchecked null dereference leading
to undefined behavior / a crash instead of a well-defined exception.

This adds a null check that throws an itk::ExceptionObject when the
transform is not invertible, matching the documented behavior of the
method.

Fixes B25 in #6575.

This was found while backporting SimpleITK PR #2721
(Transform::ApplyToImageMetadata) to SimpleITK's release branch, which
pins ITK to v5.4.7. SimpleITK's TransformTest.ApplyToImageMetadata unit
test exercises this exact case (EXPECT_ANY_THROW on a singular
transform), and segfaults without this fix.

Cherry-picked cleanly from 4365356 with a
one-line conflict resolution (an unrelated const qualifier added on
release-5.4's version of the same line).

@github-actions github-actions Bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances area:Core Issues affecting the Core module labels Sep 28, 2026
@blowekamp blowekamp added this to the ITK 5.4.8 milestone Sep 28, 2026
@blowekamp
blowekamp marked this pull request as ready for review September 28, 2026 20:00
@greptile-apps

greptile-apps Bot commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Adds null check to image metadata transform code.

The PR appears safe to merge, though the exception path still lacks a regression test.

Findings

  1. P2 Missing regression test ▶
Summary

This backport checks the inverse transform before applying it to image metadata, replacing a null dereference with an ITK exception for non-invertible transforms.

  • The existing ITK tests do not exercise the new exception path.

Reviews (2) · Last reviewed commit: "BUG: Fix possible undefined behavior in ..."

Comment on lines +474 to +478
if (inverse.IsNull())
{
itkExceptionMacro(
"ApplyToImageMetadata was invoked with non-invertible transform of type: " << this->GetNameOfClass());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Missing regression test The new exception path is not covered by a test. The existing ApplyToImageMetadata test uses an invertible transform, and the singular-transform test checks only inverse retrieval. A test that calls ApplyToImageMetadata with a singular transform and expects an itk::ExceptionObject would catch a future return of the crash this change fixes.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@blowekamp

Copy link
Copy Markdown
Member Author

Closing/reopening to retrigger ITK.macOS.Python, which failed with an unrelated compiler crash (clang segfault compiling Modules/ThirdParty/KWSys/src/KWSys/Status.cxx, unrelated to this change) rather than a real build/test failure.

@blowekamp blowekamp closed this Sep 28, 2026
@blowekamp blowekamp reopened this Sep 28, 2026
@dzenanz

dzenanz commented Sep 28, 2026

Copy link
Copy Markdown
Member

You can trigger rerun of azure pipeline by posting a comment /azp run ITK.macOS.Python.

@blowekamp
blowekamp merged commit f51ee1e into InsightSoftwareConsortium:release-5.4 Sep 29, 2026
28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:Core Issues affecting the Core module type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants