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: Richard Guenther <richard dot guenther at gmail 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:57:34 +0200
- 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>
On Tue, May 19, 2009 at 2:44 PM, Andrew Haley <aph@redhat.com> 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.
No it doesn't.
> Casting via void* just shuts up a warning; the bad code is still there.
> But this case should be fine AFAICS, because pc_begin is char[]. ?I'm not
> sure why we get the warning.
Because pc_begin being char[] doesn't make the access valid.
The patch is ok.
Thanks,
Richard.