* Re: libiberty/md5: fix strict alias warnings
[not found] <201207271436.09109.vapier@gentoo.org>
@ 2012-07-31 9:02 ` Mike Frysinger
2012-07-31 20:57 ` Matthias Klose
0 siblings, 1 reply; 3+ messages in thread
From: Mike Frysinger @ 2012-07-31 9:02 UTC (permalink / raw)
To: binutils
[-- Attachment #1: Type: Text/Plain, Size: 2591 bytes --]
[i've pushed this fix into the binutils repo since it has been merged into gcc]
Current libiberty md5 code triggers these warnings with gcc-4.7.1 for me:
libiberty/md5.c: In function ‘md5_finish_ctx’:
libiberty/md5.c:117:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
libiberty/md5.c:118:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
The change below fixes things for me. The optimized output (-O2) is the same
before/after my change on x86_64-linux. I imagine it'll be the same for most
targets. It seems simpler than using a union on the md5_ctx buffer since these
are the only two locations in the code where this occurs.
diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog
index 349dc98..6615047 100644
--- a/libiberty/ChangeLog
+++ b/libiberty/ChangeLog
@@ -1,3 +1,8 @@
+2012-07-31 Mike Frysinger <vapier@gentoo.org>
+
+ * md5.c (md5_finish_ctx): Declare swap_bytes. Assign SWAP() output
+ to swap_bytes, and then call memcpy to move it to ctx->buffer.
+
2012-07-26 Kazu Hirata <kazu@codesourcery.com>
Sandra Loosemore <sandra@codesourcery.com>
diff --git a/libiberty/md5.c b/libiberty/md5.c
index 0db8fc8..8cc0cb5 100644
--- a/libiberty/md5.c
+++ b/libiberty/md5.c
@@ -103,6 +103,7 @@ md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
{
/* Take yet unprocessed bytes into account. */
md5_uint32 bytes = ctx->buflen;
+ md5_uint32 swap_bytes;
size_t pad;
/* Now count remaining bytes. */
@@ -113,10 +114,13 @@ md5_finish_ctx (struct md5_ctx *ctx, void *resbuf)
pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
memcpy (&ctx->buffer[bytes], fillbuf, pad);
- /* Put the 64-bit file length in *bits* at the end of the buffer. */
- *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3);
- *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3) |
- (ctx->total[0] >> 29));
+ /* Put the 64-bit file length in *bits* at the end of the buffer.
+ Use memcpy to avoid aliasing problems. On most systems, this
+ will be optimized away to the same code. */
+ swap_bytes = SWAP (ctx->total[0] << 3);
+ memcpy (&ctx->buffer[bytes + pad], &swap_bytes, sizeof (swap_bytes));
+ swap_bytes = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29));
+ memcpy (&ctx->buffer[bytes + pad + 4], &swap_bytes, sizeof (swap_bytes));
/* Process last bytes. */
md5_process_block (ctx->buffer, bytes + pad + 8, ctx);
--
1.7.9.7
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 836 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: libiberty/md5: fix strict alias warnings
2012-07-31 9:02 ` libiberty/md5: fix strict alias warnings Mike Frysinger
@ 2012-07-31 20:57 ` Matthias Klose
2012-08-01 2:50 ` Mike Frysinger
0 siblings, 1 reply; 3+ messages in thread
From: Matthias Klose @ 2012-07-31 20:57 UTC (permalink / raw)
To: Mike Frysinger; +Cc: binutils, Tristan Gingold
Please could you push this to the 2.23 branch too?
On 31.07.2012 11:02, Mike Frysinger wrote:
> [i've pushed this fix into the binutils repo since it has been merged into
> gcc]
>
> Current libiberty md5 code triggers these warnings with gcc-4.7.1 for me:
>
> libiberty/md5.c: In function âmd5_finish_ctxâ: libiberty/md5.c:117:3:
> warning: dereferencing type-punned pointer will break strict-aliasing rules
> [-Wstrict-aliasing] libiberty/md5.c:118:3: warning: dereferencing
> type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
>
> The change below fixes things for me. The optimized output (-O2) is the
> same before/after my change on x86_64-linux. I imagine it'll be the same
> for most targets. It seems simpler than using a union on the md5_ctx
> buffer since these are the only two locations in the code where this
> occurs.
>
> diff --git a/libiberty/ChangeLog b/libiberty/ChangeLog index
> 349dc98..6615047 100644 --- a/libiberty/ChangeLog +++
> b/libiberty/ChangeLog @@ -1,3 +1,8 @@ +2012-07-31 Mike Frysinger
> <vapier@gentoo.org> + + * md5.c (md5_finish_ctx): Declare swap_bytes.
> Assign SWAP() output + to swap_bytes, and then call memcpy to move it to
> ctx->buffer. + 2012-07-26 Kazu Hirata <kazu@codesourcery.com> Sandra
> Loosemore <sandra@codesourcery.com>
>
> diff --git a/libiberty/md5.c b/libiberty/md5.c index 0db8fc8..8cc0cb5
> 100644 --- a/libiberty/md5.c +++ b/libiberty/md5.c @@ -103,6 +103,7 @@
> md5_finish_ctx (struct md5_ctx *ctx, void *resbuf) { /* Take yet
> unprocessed bytes into account. */ md5_uint32 bytes = ctx->buflen; +
> md5_uint32 swap_bytes; size_t pad;
>
> /* Now count remaining bytes. */ @@ -113,10 +114,13 @@ md5_finish_ctx
> (struct md5_ctx *ctx, void *resbuf) pad = bytes >= 56 ? 64 + 56 - bytes :
> 56 - bytes; memcpy (&ctx->buffer[bytes], fillbuf, pad);
>
> - /* Put the 64-bit file length in *bits* at the end of the buffer. */ -
> *(md5_uint32 *) &ctx->buffer[bytes + pad] = SWAP (ctx->total[0] << 3); -
> *(md5_uint32 *) &ctx->buffer[bytes + pad + 4] = SWAP ((ctx->total[1] << 3)
> | - (ctx->total[0] >> 29)); + /* Put the 64-bit file length in
> *bits* at the end of the buffer. + Use memcpy to avoid aliasing
> problems. On most systems, this + will be optimized away to the same
> code. */ + swap_bytes = SWAP (ctx->total[0] << 3); + memcpy
> (&ctx->buffer[bytes + pad], &swap_bytes, sizeof (swap_bytes)); +
> swap_bytes = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); + memcpy
> (&ctx->buffer[bytes + pad + 4], &swap_bytes, sizeof (swap_bytes));
>
> /* Process last bytes. */ md5_process_block (ctx->buffer, bytes + pad + 8,
> ctx);
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: libiberty/md5: fix strict alias warnings
2012-07-31 20:57 ` Matthias Klose
@ 2012-08-01 2:50 ` Mike Frysinger
0 siblings, 0 replies; 3+ messages in thread
From: Mike Frysinger @ 2012-08-01 2:50 UTC (permalink / raw)
To: Matthias Klose; +Cc: binutils, Tristan Gingold
[-- Attachment #1: Type: Text/Plain, Size: 120 bytes --]
On Tuesday 31 July 2012 16:55:40 Matthias Klose wrote:
> Please could you push this to the 2.23 branch too?
done
-mike
[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 836 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2012-08-01 2:50 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <201207271436.09109.vapier@gentoo.org>
2012-07-31 9:02 ` libiberty/md5: fix strict alias warnings Mike Frysinger
2012-07-31 20:57 ` Matthias Klose
2012-08-01 2:50 ` Mike Frysinger
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 read-only IMAP folder(s) and NNTP newsgroup(s).