BUG: Backport null checks after ReadHeader in IPLCommonImageIO#6064
BUG: Backport null checks after ReadHeader in IPLCommonImageIO#6064hjmjohnson wants to merge 1 commit intoInsightSoftwareConsortium:release-5.4from
Conversation
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.
|
| 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
Reviews (1): Last reviewed commit: "BUG: Add null checks after ReadHeader in..." | Re-trigger Greptile
| if (curImageHeader == nullptr) | ||
| { | ||
| itkExceptionMacro("ReadHeader failed for " << fullPath); | ||
| } |
There was a problem hiding this comment.
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:
| if (curImageHeader == nullptr) | |
| { | |
| itkExceptionMacro("ReadHeader failed for " << fullPath); | |
| } | |
| if (curImageHeader == nullptr) | |
| { | |
| continue; | |
| } |
There was a problem hiding this comment.
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.
Cherry-pick of 0eb1c42 from main. Adds null checks after
ReadHeadercalls inIPLCommonImageIOto prevent null pointer dereference.Backport for #6051 (Tier 1).