[cfe-dev] -ftrivial-auto-var-init=pattern vs =uninitialized and union initialization

Vitaly Buka via cfe-dev cfe-dev at lists.llvm.org
Mon Sep 23 14:20:29 PDT 2019


Hi everyone,

I am trying to enable -ftrivial-auto-var-init=pattern on various code and
noticed inconvenient inconsistency with code like [1].

According standard, as I understand, only the first member of the union
must be initialized and the tail of the second member can stay
uninitialized. If we use -ftrivial-auto-var-init=pattern it fills the tail
using our pattern. However by default GCC, clang (without
-ftrivial-auto-var-init), msvc all initialize entire union with 0s. Not
sure if it's just coincidence or guaranteed feature.

So -ftrivial-auto-var-init=pattern breaks such code. Especially bad if you
don't know that U is a union and ={} looks good there.

Should we consider giving up here and using zeroes for union tails even
with -ftrivial-auto-var-init=pattern?

1. Example:
union U {
  char small[2];
  char large[100];
};
void f(void*);
void test() {
  union U u = {};
  f(&u);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190923/18f531e9/attachment.html>


More information about the cfe-dev mailing list