Is this "Invalid union access“ reported by Coverity valid ? Does it pose any real issue ? #5219
-
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
I would treat this as likely a false positive unless Coverity gives a concrete path where the stored type tag and the accessed union member can really diverge.
That pattern is valid when the tag invariant is preserved, but it is also a common source of static-analysis warnings because analyzers may not be able to prove that every access is guarded by the same tag that constructed the active member. So my practical read is:
If the CIDs all point to the normal |
Beta Was this translation helpful? Give feedback.
-
|
Followed up on this and traced the tagged-union invariant through the source (constructors, (Drafted by Claude Code.) |
Beta Was this translation helpful? Give feedback.

I would treat this as likely a false positive unless Coverity gives a concrete path where the stored type tag and the accessed union member can really diverge.
nlohmann::jsonintentionally uses a tagged-union style representation. The important invariant is that the active member inm_valueis determined bym_type— for example, object values usem_value.object, arrays usem_value.array, strings usem_value.string, and so on. Destruction/copy/move paths switch on the storedvalue_tand then access the corresponding union member.That pattern is valid when the tag invariant is preserved, but it is also a common source of static-analysis warnings because analyzers may not be able to prove t…