From patchwork Thu Aug 6 10:25:58 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikael Morin X-Patchwork-Id: 504639 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]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 648AD1402AB for ; Thu, 6 Aug 2015 20:26:22 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=bON4F1hP; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :subject:to:message-id:date:mime-version:content-type; q=dns; s= default; b=vjocoYyb35PfhG8li3SYXdMRwvQzZH92p1gsu883jY2eg/+DcagDm nDcBPIm8Us2tKpBMU3uoJSK8Knn1OlSoXmKi+rKum/bSwnbFIAiBSqrJBTkn/gpQ 4ivlEJT7nkmc8naXGTrw/JQUhYhOi0U8msvyF+tMzT0X/7D/YWWQDo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :subject:to:message-id:date:mime-version:content-type; s= default; bh=2uFItfFhc0vfT0DgbUV/4xWsA2Y=; b=bON4F1hPMdwQ9oEorMOe sBY84ddAkRf0rp5KeIWZuxHaea9ZEhxOvqnp3o0qEc1lx0Q1pg0q3mZWdcGq6opU R+LvYkmIhJcWAwXCFDQADyNkr7CPBh54c+YaDauoitSZQexj71MvCDA/MomBfCC4 O5Sxn41MZxSOcuVBlLrAQhw= 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-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@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, 6 Aug 2015 12:25:58 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 MIME-Version: 1.0 X-IsSubscribed: yes Hello, this avoids an error found with bootstrap-ubsan. Regression tested on x86_64-unknown-linux-gnu. OK for trunk? Mikael 2015-08-05 Mikael Morin * hwint.h (sext_hwi): Rewrite without undefined behaviour on negative SRC. 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); } }