BUG: Fix possible undefined behavior in Transform::ApplyToImageMetadata - #6914
Conversation
See B25 in issue InsightSoftwareConsortium#6575. (cherry picked from commit 4365356)
|
| if (inverse.IsNull()) | ||
| { | ||
| itkExceptionMacro( | ||
| "ApplyToImageMetadata was invoked with non-invertible transform of type: " << this->GetNameOfClass()); | ||
| } |
There was a problem hiding this comment.
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!
|
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. |
|
You can trigger rerun of azure pipeline by posting a comment |
f51ee1e
into
InsightSoftwareConsortium:release-5.4
Backport of #6583 to release-5.4.
itk::Transform::ApplyToImageMetadata()callsthis->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 leadingto undefined behavior / a crash instead of a well-defined exception.
This adds a null check that throws an
itk::ExceptionObjectwhen thetransform 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'sreleasebranch, whichpins ITK to v5.4.7. SimpleITK's
TransformTest.ApplyToImageMetadataunittest exercises this exact case (
EXPECT_ANY_THROWon a singulartransform), and segfaults without this fix.
Cherry-picked cleanly from 4365356 with a
one-line conflict resolution (an unrelated
constqualifier added onrelease-5.4's version of the same line).