Skip to content

Value-initialize the result local in generated C++ tagged-enum constructors#1167

Open
sylvestre wants to merge 1 commit into
mozilla:mainfrom
sylvestre:cxx-tagged-enum-value-init
Open

Value-initialize the result local in generated C++ tagged-enum constructors#1167
sylvestre wants to merge 1 commit into
mozilla:mainfrom
sylvestre:cxx-tagged-enum-value-init

Conversation

@sylvestre

@sylvestre sylvestre commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Emit T result{}; instead of T result; in the generated static variant constructors so the payload union is zero-initialized rather than left indeterminate. For no-payload variants only the tag is set, so the previously-uninitialized union produced false "uninitialized scalar" reports from static-analysis tools (e.g. Coverity). Payload variants are unaffected: the union is value-initialized and the payload is then constructed in place via placement new as before.

Add a regression test (tagged_enum_value_init) covering no-payload and payload variants, and regenerate the affected C++ expectations.

This will remove about 110 CID in coverity for firefox

…uctors

Emit `T result{};` instead of `T result;` in the generated static variant
constructors so the payload union is zero-initialized rather than left
indeterminate. For no-payload variants only the tag is set, so the
previously-uninitialized union produced false "uninitialized scalar"
reports from static-analysis tools (e.g. Coverity). Payload variants are
unaffected: the union is value-initialized and the payload is then
constructed in place via placement new as before.

Add a regression test (tagged_enum_value_init) covering no-payload and
payload variants, and regenerate the affected C++ expectations.

@emilio emilio left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@sylvestre I'm confused, why do we want this?

We're methodically initializing the fields right below... So it seems like at worst we're doing more work, and if we're lucky we're only giving more work to the compiler to optimize it out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants