BUG: Backport value-initialize singleton instances#6065
BUG: Backport value-initialize singleton instances#6065hjmjohnson wants to merge 1 commit intoInsightSoftwareConsortium:release-5.4from
Conversation
The Singleton<T>() template used `new T` (default-initialization) which
leaves POD types like bool and unsigned int with indeterminate values.
While the itkGetGlobalInitializeMacro immediately assigns the intended
value on first creation, the analyzer cannot trace through the singleton
index indirection and correctly flags the intermediate indeterminate
state.
Change to `new T{}` (value-initialization) which zero-initializes POD
types, eliminating undefined behavior and resolving:
- core.uninitialized.UndefReturn in itkDataObject.cxx:142
- core.uninitialized.UndefReturn in itkObject.cxx:452
- core.uninitialized.UndefReturn in itkMetaImageIO.cxx:940
- core.UndefinedBinaryOperatorResult in itkDataObject.cxx:131
Addresses scan-build findings from issue InsightSoftwareConsortium#1261.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
| Filename | Overview |
|---|---|
| Modules/Core/Common/include/itkSingleton.h | Single-character fix: new T → new T{} in Singleton<T>() ensures value-initialization for all instantiated types, eliminating potential indeterminate values for POD/scalar singletons. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["Singleton<T>(globalName, deleteFunc)"] --> B["GetGlobalInstance<T>(globalName)"]
B --> C{instance == nullptr?}
C -- "No" --> F["return existing instance"]
C -- "Yes (before fix)" --> D_old["new T\n(default-init: indeterminate for scalars/POD)"]
C -- "Yes (after fix)" --> D_new["new T{}\n(value-init: zero-initialized for scalars/POD)"]
D_old --> E["SetGlobalInstance + register deleteFunc"]
D_new --> E
E --> F
Reviews (1): Last reviewed commit: "BUG: Value-initialize singleton instance..." | Re-trigger Greptile
Cherry-pick of 2f37d7a from main. Value-initializes singleton instances to avoid indeterminate values from default-initialization.
Backport for #6051 (Tier 1).