Skip to content

BUG: Backport null checks after ReadHeader in IPLCommonImageIO#6064

Merged
hjmjohnson merged 1 commit intoInsightSoftwareConsortium:release-5.4from
hjmjohnson:backport-ipl-null-checks
Apr 16, 2026
Merged

BUG: Backport null checks after ReadHeader in IPLCommonImageIO#6064
hjmjohnson merged 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 thread Modules/IO/IPL/src/itkIPLCommonImageIO.cxx
@thewtex thewtex added this to the ITK 5.4.6 milestone Apr 15, 2026
@hjmjohnson hjmjohnson merged commit 8505c1a into InsightSoftwareConsortium:release-5.4 Apr 16, 2026
19 of 22 checks passed
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