[llvm] r174827 - ok, ok, stop fighting type punning warnings by just using a union.

David Blaikie dblaikie at gmail.com
Sun Feb 10 00:45:09 PST 2013


On Feb 9, 2013 10:38 PM, "Chris Lattner" <sabre at nondot.org> wrote:
>
> Author: lattner
> Date: Sun Feb 10 00:36:29 2013
> New Revision: 174827
>
> URL: http://llvm.org/viewvc/llvm-project?rev=174827&view=rev
> Log:
> ok, ok, stop fighting type punning warnings by just using a union.
>
> Modified:
>     llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
>
> Modified: llvm/trunk/include/llvm/Bitcode/BitstreamReader.h
> URL:
http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/BitstreamReader.h?rev=174827&r1=174826&r2=174827&view=diff
>
==============================================================================
> --- llvm/trunk/include/llvm/Bitcode/BitstreamReader.h (original)
> +++ llvm/trunk/include/llvm/Bitcode/BitstreamReader.h Sun Feb 10 00:36:29
2013
> @@ -364,13 +364,16 @@ public:
>      uint32_t R = uint32_t(CurWord);
>
>      // Read the next word from the stream.
> -    char buf[sizeof(word_t)] = {0};
> -    BitStream->getBitcodeBytes().readBytes(NextChar, sizeof(buf),
> -                                           (uint8_t*)buf, NULL);
> +    union {
> +      uint8_t ArrayMember[sizeof(word_t)];
> +      support::detail::packed_endian_specific_integral
> +      <word_t, support::little, support::unaligned> EndianMember;
> +    } buf = { { 0 } };
>
> -    typedef support::detail::packed_endian_specific_integral
> -       <word_t, support::little, support::unaligned> Endian_T;
> -    CurWord = *(Endian_T*)buf;
> +    BitStream->getBitcodeBytes().readBytes(NextChar, sizeof(buf),
> +                                           buf.ArrayMember, NULL);
> +    // Handle big-endian byte-swapping if necessary.
> +    CurWord = buf.EndianMember;

This is still invalid C++ isn't it (UB to read from the non active member
of a union) - why not just use memcpy, the language-sanctioned way to do
this operation?

>
>      NextChar += sizeof(word_t);
>
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130210/d487050a/attachment.html>


More information about the llvm-commits mailing list