Fix Alias Bug
authorDavid Greene <greened@obbligato.org>
Thu, 10 Jan 2013 18:17:54 +0000 (18:17 +0000)
committerDavid Greene <greened@obbligato.org>
Thu, 10 Jan 2013 18:17:54 +0000 (18:17 +0000)
Use memcpy to do type punning instead of a cast.  A cast or similar
operation through a union breaks strict aliasing rules.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@172081 91177308-0d34-0410-b5e6-96231b3b80d8

unittests/Support/YAMLIOTest.cpp

index afa71cc25ea2b1b695805e1d7534c5c27ec6ab68..a39fbebf295d2f7c378bae54d2230359b708ecc5 100644 (file)
@@ -783,10 +783,18 @@ namespace yaml {
     static void mapping(IO &io, KindAndFlags& kf) {
       io.mapRequired("kind",  kf.kind);
       // type of flags field varies depending on kind field
-      if ( kf.kind == kindA )
-        io.mapRequired("flags", *((AFlags*)&kf.flags));
-      else
-        io.mapRequired("flags", *((BFlags*)&kf.flags));
+
+      // Use memcpy here to avoid breaking strict aliasing rules.
+      if ( kf.kind == kindA ) {
+        AFlags aflags;
+        memcpy(&aflags, &kf.flags, sizeof(aflags));
+        io.mapRequired("flags", aflags);
+      }
+      else {
+        BFlags bflags;
+        memcpy(&bflags, &kf.flags, sizeof(bflags));
+        io.mapRequired("flags", bflags);
+      }
     }
   };
 }