All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Paweł Sikora" <pluto@agmk.net>
To: linux-raid@vger.kernel.org
Cc: Jes.Sorensen@redhat.com
Subject: FYI: Re: ugly way to avoiding gcc strict-aliasing warning?
Date: Wed, 18 Jan 2012 19:08:15 +0100	[thread overview]
Message-ID: <1926832.RlMyFfyk5p@vmx> (raw)

[-- Attachment #1: Type: text/plain, Size: 0 bytes --]



[-- Attachment #2: Richard Guenther <rguenther@suse.de>: Re: ugly way to avoiding gcc strict-aliasing warning? --]
[-- Type: message/rfc822, Size: 3188 bytes --]

[-- Attachment #2.1.1: Type: text/plain, Size: 1448 bytes --]

On Tue, 17 Jan 2012, Paweł Sikora wrote:

> Hi,
> 
> recently i've seen a tricky commit in the mdadm git repository:
> http://neil.brown.name/git?p=mdadm;a=commit;h=90fa1a292929ff8a6c7357254b6f616608ec01b5
> 
> imho, this is a tricky way to avoid gcc-4.7 diagnostics machinery
> but it still violates aliasing rules. am i right?
> 
> thanks in advance for any comments.

#ifndef AVOID_WARNING
  *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP (ctx->total[0] << 
3);
#else
  ptr = (md5_uint32 *) &ctx->buffer[bytes + pad + 4];
  *ptr = SWAP (ctx->total[0] << 3);
#endif

Both code-snippets are indeed exactly the same.  In recent GCC
(>= 4.5), if you ever read from ctx->buffer[] with an effective
type other than md5_uint32 after writing to it using that type
you can get miscompilations (reading via an effective type
of char is ok, which happens to be the type of ctx->buffer).

In older GCC the above can result in miscompiles even if you
happen to follow that restriction (thus, later GCC got more
permissive in this area).  A portable and safe way of doing the above is

 md5_uint32 tem = SWAP (ctx->total[0] << 3);
 memcpy (&ctx->buffer[bytes + pad + 4], &tem, sizeof (md5_uint32));

GCC will inline that memcpy.

Richard.

-- 
Richard Guenther <rguenther@suse.de>
SUSE / SUSE Labs
SUSE LINUX Products GmbH - Nuernberg - AG Nuernberg - HRB 16746
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer

                 reply	other threads:[~2012-01-18 18:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1926832.RlMyFfyk5p@vmx \
    --to=pluto@agmk.net \
    --cc=Jes.Sorensen@redhat.com \
    --cc=linux-raid@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.