linux-raid.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* FYI: Re: ugly way to avoiding gcc strict-aliasing warning?
@ 2012-01-18 18:08 Paweł Sikora
  0 siblings, 0 replies; only message in thread
From: Paweł Sikora @ 2012-01-18 18:08 UTC (permalink / raw)
  To: linux-raid; +Cc: Jes.Sorensen

[-- 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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2012-01-18 18:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-01-18 18:08 FYI: Re: ugly way to avoiding gcc strict-aliasing warning? Paweł Sikora

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).