Skip to content

BUG: Backport null checks after ReadHeader in IPLCommonImageIO#6064

Open
hjmjohnson wants to merge 1 commit intoInsightSoftwareConsortium:release-5.4from
hjmjohnson:backport-ipl-null-checks
Open

BUG: Backport null checks after ReadHeader in IPLCommonImageIO#6064
hjmjohnson wants to merge 1 commit intoInsightSoftwareConsortium:release-5.4from
hjmjohnson:backport-ipl-null-checks

Conversation

@hjmjohnson
Copy link
Copy Markdown
Member

Cherry-pick of 0eb1c42 from main. Adds null checks after ReadHeader calls in IPLCommonImageIO to prevent null pointer dereference.

Backport for #6051 (Tier 1).

The base class ReadHeader returns nullptr, and subclass overrides may
also return null without throwing. Two call sites in ReadImageInformation
dereference the result without null checks:

1. m_ImageHeader->modality (line 140) — null dereference when ReadHeader
   fails silently. Now throws itkExceptionMacro on null return.
2. curImageHeader->examNumber (line 223) — null dereference in the
   directory scan loop. Now skips null results with continue.

Addresses nullability.NullPassedToNonnull and core.NullDereference
findings from issue InsightSoftwareConsortium#1261.
@github-actions github-actions bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances area:IO Issues affecting the IO module labels Apr 15, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 15, 2026

Greptile Summary

This PR backports commit 0eb1c42d from main to release-5.4, adding two explicit null checks after ReadHeader calls in IPLCommonImageIO::ReadImageInformation() to prevent null pointer dereferences. Previously the code relied on an incorrect assumption (noted in a comment) that exceptions would propagate naturally, but if a child class's ReadHeader returned nullptr without throwing, the code would crash with undefined behaviour.

Confidence Score: 5/5

Safe to merge — the fix eliminates a confirmed null-pointer crash with well-defined exception handling; remaining P2 note is a style/philosophy point that doesn't affect correctness in practice.

Both null checks are strictly safer than the original code. The first check (primary file) is unambiguously correct. The second check (directory loop) trades a crash for an exception; the suggestion to use continue instead is a minor philosophy concern since all known child classes already throw on failure anyway. No regressions, no security concerns, and the fix aligns with the cherry-pick from main.

Modules/IO/IPL/src/itkIPLCommonImageIO.cxx — specifically the second null check at line 228 and whether throw vs continue is the right policy for the directory-scan loop.

Important Files Changed

Filename Overview
Modules/IO/IPL/src/itkIPLCommonImageIO.cxx Adds two null checks after ReadHeader calls; first check (primary file) is straightforward and correct; second check (directory loop) throws an exception that propagates outside the surrounding try-catch, which may be overly strict compared to just continuing the loop.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[ReadImageInformation called] --> B["ReadHeader(primaryFile)"]
    B --> C{nullptr?}
    C -- yes --> D["itkExceptionMacro NEW\n(was: null-deref crash)"]
    C -- no --> E[Read modality, add to list\nloop through directory]
    E --> F["ReadHeader(curFile) in try-catch"]
    F -- exception --> G[catch: continue to next file]
    F -- nullptr --> H{nullptr? NEW}
    H -- yes --> I["itkExceptionMacro\n(propagates OUTSIDE try-catch)"]
    H -- no --> J[Compare series/echo keys]
    J --> K[AddElementToList or skip]
    K --> F
Loading

Reviews (1): Last reviewed commit: "BUG: Add null checks after ReadHeader in..." | Re-trigger Greptile

Comment on lines +228 to +231
if (curImageHeader == nullptr)
{
itkExceptionMacro("ReadHeader failed for " << fullPath);
}
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 Loop null-check aborts scan on one unexpected file

The null check correctly prevents a crash, but the itkExceptionMacro throw sits outside the enclosing try-catch block (lines 217-227). If any file in the directory has a ReadHeader implementation that returns nullptr without throwing — the base class IPLCommonImageIO::ReadHeader does exactly this — the exception will propagate all the way up and abort the entire ReadImageInformation() call, rather than skipping that file as the surrounding try-catch intends.

All three known child-class implementations (GE4, GE5, GEAdw) always throw on failure, so this is unlikely to trigger in practice. But using continue here would be consistent with the loop's error-handling philosophy and safe against future subclasses:

Suggested change
if (curImageHeader == nullptr)
{
itkExceptionMacro("ReadHeader failed for " << fullPath);
}
if (curImageHeader == nullptr)
{
continue;
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is a backport from main -> release-5.4; if another developer thinks it needs to be fixed, then we need to fix it upstream as well.

@thewtex thewtex added this to the ITK 5.4.6 milestone Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:IO Issues affecting the IO 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.

3 participants