This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Type-punning warnings [was Re: PATCH: silence warnings in unwind-dw2-fde.c]
- From: Dave Korn <dave dot korn dot cygwin at googlemail dot com>
- To: Andrew Haley <aph at redhat dot com>
- Cc: Dave Korn <dave dot korn dot cygwin at googlemail dot com>, "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Tue, 19 May 2009 14:11:31 +0100
- Subject: Re: Type-punning warnings [was Re: PATCH: silence warnings in unwind-dw2-fde.c]
- References: <1242693457.16289.13.camel@helios> <4A12A884.9030708@gmail.com> <4A12A9C3.6080607@redhat.com>
Andrew Haley wrote:
> Dave Korn wrote:
>> Ben Elliston wrote:
>>> This patch silences the following warnings when building libgcc:
>>>
>>> unwind-dw2-fde.c:321: warning: dereferencing type-punned pointer will break
>>> strict-aliasing rules
>>> - const _Unwind_Ptr x_ptr = *(const _Unwind_Ptr *) x->pc_begin;
>>> - const _Unwind_Ptr y_ptr = *(const _Unwind_Ptr *) y->pc_begin;
>>> + _Unwind_Ptr x_ptr, y_ptr;
>>> + memcpy (&x_ptr, x->pc_begin, sizeof (_Unwind_Ptr));
>>> + memcpy (&y_ptr, y->pc_begin, sizeof (_Unwind_Ptr));
>> Say, I've been in the habit in the past (GCC 3.x) of avoiding these warnings
>> by doing like:
>>
>>> - const _Unwind_Ptr x_ptr = *(const _Unwind_Ptr *) x->pc_begin;
>>> - const _Unwind_Ptr y_ptr = *(const _Unwind_Ptr *) y->pc_begin;
>>> + const _Unwind_Ptr x_ptr = *(const _Unwind_Ptr *) (void *) x->pc_begin;
>>> + const _Unwind_Ptr y_ptr = *(const _Unwind_Ptr *) (void *) y->pc_begin;
>> ... which I see no longer works. Was that ever correct, or was I just losing
>> information during the cast-to-void that meant GCC just couldn't diagnose a
>> potential problem, but it was still there? I believed that casting through
>> void would make the result could-alias-anything, which I thought would prevent
>> any untoward consequences.
>
> Casting via void* just shuts up a warning; the bad code is still there.
Oh, ow.
> But this case should be fine AFAICS, because pc_begin is char[]. I'm not
> sure why we get the warning.
And casting through void just doubles the number of warnings in this case!
cheers,
DaveK