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.

llvm-svn: 172081
This commit is contained in:
David Greene 2013-01-10 18:17:54 +00:00
parent 37c528259a
commit 4162c2d31b
1 changed files with 12 additions and 4 deletions

View 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);
}
}
};
}