Skip to content

BUG: Backport value-initialize singleton instances#6065

Open
hjmjohnson wants to merge 1 commit intoInsightSoftwareConsortium:release-5.4from
hjmjohnson:backport-singleton-value-init
Open

BUG: Backport value-initialize singleton instances#6065
hjmjohnson wants to merge 1 commit intoInsightSoftwareConsortium:release-5.4from
hjmjohnson:backport-singleton-value-init

Conversation

@hjmjohnson
Copy link
Copy Markdown
Member

Cherry-pick of 2f37d7a from main. Value-initializes singleton instances to avoid indeterminate values from default-initialization.

Backport for #6051 (Tier 1).

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>
@github-actions github-actions bot added type:Bug Inconsistencies or issues which will cause an incorrect result under some or all circumstances area:Core Issues affecting the Core module labels Apr 15, 2026
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 15, 2026

Greptile Summary

This PR backports a single-line fix from main (cherry-pick of 2f37d7a3) that changes new T to new T{} in the Singleton<T> template function in itkSingleton.h. The fix ensures value-initialization instead of default-initialization, preventing indeterminate values for types without user-defined constructors (e.g., scalar types, POD structs, aggregates).

Confidence Score: 5/5

Safe to merge — minimal, correct bug fix with no side effects for types with user-defined constructors.

The single-line change from new T to new T{} is semantically identical for types with user-defined default constructors (the constructor is called in both cases) and is strictly safer for scalar/POD types (ensures zero-initialization vs. indeterminate values). No regressions are possible, and the fix correctly addresses the reported bug. The GlobalObject struct members already use brace-initialization, making this change consistent with the rest of the file.

No files require special attention.

Important Files Changed

Filename Overview
Modules/Core/Common/include/itkSingleton.h Single-character fix: new Tnew 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
Loading

Reviews (1): Last reviewed commit: "BUG: Value-initialize singleton instance..." | Re-trigger Greptile

@hjmjohnson hjmjohnson requested a review from thewtex April 15, 2026 14:42
Copy link
Copy Markdown
Member

@thewtex thewtex left a comment

Choose a reason for hiding this comment

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

@hjmjohnson thank you!

@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:Core Issues affecting the Core 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