[llvm-commits] [llvm] r172081 - /llvm/trunk/unittests/Support/YAMLIOTest.cpp

David Greene greened at obbligato.org
Thu Jan 10 10:17:54 PST 2013


Author: greened
Date: Thu Jan 10 12:17:54 2013
New Revision: 172081

URL: http://llvm.org/viewvc/llvm-project?rev=172081&view=rev
Log:
Fix Alias Bug

Use memcpy to do type punning instead of a cast.  A cast or similar
operation through a union breaks strict aliasing rules.

Modified:
    llvm/trunk/unittests/Support/YAMLIOTest.cpp

Modified: llvm/trunk/unittests/Support/YAMLIOTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/YAMLIOTest.cpp?rev=172081&r1=172080&r2=172081&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/YAMLIOTest.cpp (original)
+++ llvm/trunk/unittests/Support/YAMLIOTest.cpp Thu Jan 10 12:17:54 2013
@@ -783,10 +783,18 @@
     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);
+      }
     }
   };
 }





More information about the llvm-commits mailing list