[llvm-commits] [PATCH] Fix Alias Bug
David Blaikie
dblaikie at gmail.com
Tue Jan 8 13:29:51 PST 2013
General feedback: please send all patches for review as attachments,
not inline, so that mail clients don't mangle your patches.
On Tue, Jan 8, 2013 at 1:15 PM, David Greene <dag at cray.com> wrote:
> Use a union to do type punning instead of a cast. A cast
> breaks strict aliasing rules.
> ---
> llvm/unittests/Support/YAMLIOTest.cpp | 10 ++++++++--
> 1 files changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp
> index afa71cc..f148c8d 100644
> --- a/llvm/unittests/Support/YAMLIOTest.cpp
> +++ b/llvm/unittests/Support/YAMLIOTest.cpp
> @@ -783,10 +783,16 @@ namespace yaml {
> static void mapping(IO &io, KindAndFlags& kf) {
> io.mapRequired("kind", kf.kind);
> // type of flags field varies depending on kind field
> + union {
> + uint32_t i;
> + AFlags aflags;
> + BFlags bflags;
> + } flags = { kf.flags };
> +
It's still UB to access the non-active member of a union. I believe
the only valid way to do this sort of thing is with memcpy (you're
allowed to read/write the bytes of such simple types through char*s) &
let the compiler optimize that into the same trivial copy.
> if ( kf.kind == kindA )
> - io.mapRequired("flags", *((AFlags*)&kf.flags));
> + io.mapRequired("flags", flags.aflags);
> else
> - io.mapRequired("flags", *((BFlags*)&kf.flags));
> + io.mapRequired("flags", flags.bflags);
> }
> };
> }
> --
> 1.7.8.4
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list