From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 127291 invoked by alias); 6 Aug 2015 10:26:15 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 127254 invoked by uid 89); 6 Aug 2015 10:26:15 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.3 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_LOW,RP_MATCHES_RCVD,SPF_PASS autolearn=ham version=3.3.2 X-HELO: smtp22.services.sfr.fr Received: from smtp22.services.sfr.fr (HELO smtp22.services.sfr.fr) (93.17.128.13) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Thu, 06 Aug 2015 10:26:13 +0000 Received: from filter.sfr.fr (localhost [86.72.15.76]) by msfrf2216.sfr.fr (SMTP Server) with ESMTP id 3D0077000170 for ; Thu, 6 Aug 2015 12:26:11 +0200 (CEST) Authentication-Results: sfrmc.priv.atos.fr; dkim=none (no signature); dkim-adsp=none (no policy) header.from=mikael.morin@sfr.fr Received: from tolstoi.localhost (76.15.72.86.rev.sfr.net [86.72.15.76]) (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits)) (No client certificate requested) by msfrf2216.sfr.fr (SMTP Server) with ESMTP id 0EA267000102 for ; Thu, 6 Aug 2015 12:26:10 +0200 (CEST) X-SFR-UUID: 20150806102611600.0EA267000102@msfrf2216.sfr.fr From: Mikael Morin Subject: [Patch] sext_hwi: Avoid left shift of negative value undefined behaviour To: gcc-patches Message-ID: <55C33636.7020907@sfr.fr> Date: Thu, 06 Aug 2015 10:26:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=------------090207070604070503060704 X-IsSubscribed: yes X-SW-Source: 2015-08/txt/msg00340.txt.bz2 This is a multi-part message in MIME format. --------------090207070604070503060704 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-length: 128 Hello, this avoids an error found with bootstrap-ubsan. Regression tested on x86_64-unknown-linux-gnu. OK for trunk? Mikael --------------090207070604070503060704 Content-Type: text/plain; charset=UTF-8; name="noub_sext.CL" Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="noub_sext.CL" Content-length: 171 MjAxNS0wOC0wNSAgTWlrYWVsIE1vcmluICA8bWlrYWVsQGdjYy5nbnUub3Jn PgoKCSogaHdpbnQuaCAoc2V4dF9od2kpOiBSZXdyaXRlIHdpdGhvdXQgdW5k ZWZpbmVkIGJlaGF2aW91ciBvbgoJbmVnYXRpdmUgU1JDLgo= --------------090207070604070503060704 Content-Type: text/x-patch; name="noub_sext.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="noub_sext.diff" Content-length: 557 diff --git a/gcc/hwint.h b/gcc/hwint.h index 3793986..9c3eda0 100644 --- a/gcc/hwint.h +++ b/gcc/hwint.h @@ -246,8 +246,9 @@ sext_hwi (HOST_WIDE_INT src, unsigned int prec) else { gcc_checking_assert (prec < HOST_BITS_PER_WIDE_INT); - int shift = HOST_BITS_PER_WIDE_INT - prec; - return (src << shift) >> shift; + HOST_WIDE_INT sign_mask = HOST_WIDE_INT_1 << (prec - 1); + HOST_WIDE_INT value_mask = (HOST_WIDE_INT_1U << prec) - HOST_WIDE_INT_1U; + return (((src & value_mask) ^ sign_mask) - sign_mask); } } --------------090207070604070503060704--