Skip to content

BUG: Fix segfault when mask file does not exist (NOMASK)#1977

Merged
cookpa merged 1 commit into
ANTsX:masterfrom
gdevenyi:fix-mask-nomask-segfault
Jun 4, 2026
Merged

BUG: Fix segfault when mask file does not exist (NOMASK)#1977
cookpa merged 1 commit into
ANTsX:masterfrom
gdevenyi:fix-mask-nomask-segfault

Conversation

@gdevenyi

@gdevenyi gdevenyi commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

Problem

antsRegistration segfaults when --masks '[ NOMASK,NOMASK ]' or any non-existent mask filename is passed.

Root Cause

In getCachedMask (Examples/antsRegistrationTemplateHeader.h:354), ReadImage sets the smart pointer to nullptr when the file doesn't exist. The subsequent cached->DisconnectPipeline() unconditionally dereferences this null pointer.

Fix

Add a null check before calling DisconnectPipeline(). The downstream code (AddFixedImageMask/AddMovingImageMask) already handles null masks correctly — this was the only missing guard.

Verification

The command from the issue now fails gracefully instead of segfaulting:

antsRegistration --masks '[ NOMASK,NOMASK ]' ...

getCachedMask unconditionally called DisconnectPipeline() after ReadImage,
which sets the pointer to nullptr when the file doesn't exist. This caused
a null pointer dereference segfault when using --masks '[ NOMASK,NOMASK ]'
or any non-existent mask filename.

The documentation states non-existent mask files should be silently ignored.
Copilot AI review requested due to automatic review settings June 4, 2026 16:14

Copilot AI left a comment

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.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Adds a null-check around DisconnectPipeline() to avoid dereferencing a null cached mask image when ReadImage() does not populate it.

Changes:

  • Guard cached->DisconnectPipeline() behind cached.IsNotNull() after ReadImage().
  • Prevent potential null dereference in the cache-loading lambda.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Examples/antsRegistrationTemplateHeader.h
@gdevenyi

gdevenyi commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

I introduced this in #1970

@cookpa cookpa 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.

This restores the previous behavior

@cookpa cookpa merged commit 7ead96a into ANTsX:master Jun 4, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants