This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
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: "gcc at gcc dot gnu dot org" <gcc at gcc dot gnu dot org>
- Date: Tue, 19 May 2009 13:39:32 +0100
- Subject: Type-punning warnings [was Re: PATCH: silence warnings in unwind-dw2-fde.c]
- References: <1242693457.16289.13.camel@helios>
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.
Also, maybe rather than warning, we could translate the assignment to a
built-in memcpy internally? Or perhaps best of all do both?
cheers,
DaveK