From patchwork Fri Jul 27 18:36:08 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Mike Frysinger X-Patchwork-Id: 173759 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id E9F142C0098 for ; Sat, 28 Jul 2012 04:36:36 +1000 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1344018998; h=Comment: DomainKey-Signature:Received:Received:Received:Received:From:To: Subject:Date:User-Agent:MIME-Version:Content-Type: Content-Transfer-Encoding:Message-Id:Mailing-List:Precedence: List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=qEJSnNQWH7G2qZUd24v1IURkrCs=; b=kHk/VD2Ync+JQiv R8GHspeByk+dLI/mzzZlQJK6ybRwjHX94ARiAblta2mD0fiOFnWbe8rSEMrbJ6GR KSVm3tkPMBrCaUjQJRb/Z7CcSCoWjr2waD72MeiSeMScUVXu2YRWY4w48tL+uSwU 5aqxXQrXf7Z8/A9YrFa04zTKzuP0= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:From:To:Subject:Date:User-Agent:MIME-Version:Content-Type:Content-Transfer-Encoding:Message-Id:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=jlB8UOdO8WDkzL402pIse8TbBQfj56NkMCOFA857TJwmtboSbrIj2ARtwe06Bv PVzF+09fysLXct/BxRMAjmIDCXKc/n4od/ipVO7/qJ038Cj5GxChDsR2EXaXPd3X rtHF06RUCVWTyyRBc79/9+riBx3Hmps9cbfrei/NJ5HN8=; Received: (qmail 5723 invoked by alias); 27 Jul 2012 18:36:30 -0000 Received: (qmail 5498 invoked by uid 22791); 27 Jul 2012 18:36:27 -0000 X-SWARE-Spam-Status: No, hits=-7.8 required=5.0 tests=AWL, BAYES_00, KHOP_PGP_SIGNED, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_HI, RCVD_IN_HOSTKARMA_W, TW_CP, T_RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from smtp.gentoo.org (HELO smtp.gentoo.org) (140.211.166.183) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Fri, 27 Jul 2012 18:36:09 +0000 Received: from vapier.localnet (localhost [127.0.0.1]) by smtp.gentoo.org (Postfix) with ESMTP id C386D1B4080; Fri, 27 Jul 2012 18:36:08 +0000 (UTC) From: Mike Frysinger To: gcc-patches@gcc.gnu.org Subject: libiberty/md5: fix strict alias warnings Date: Fri, 27 Jul 2012 14:36:08 -0400 User-Agent: KMail/1.13.7 (Linux/3.5.0; KDE/4.6.5; x86_64; ; ) MIME-Version: 1.0 Message-Id: <201207271436.09109.vapier@gentoo.org> Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org 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. 2012-07-27 Mike Frysinger * md5.c (md5_finish_ctx): Declare swap_bytes. Assign SWAP() output to swap_bytes, and then call memcpy to move it to ctx->buffer. --- a/libiberty/md5.c +++ b/libiberty/md5.c @@ -102,7 +102,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, bytes = ctx->buflen; size_t pad; /* Now count remaining bytes. */ @@ -113,10 +113,13 @@ 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);