From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19687 invoked by alias); 29 Dec 2014 06:27:18 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 19677 invoked by uid 89); 29 Dec 2014 06:27:16 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL,BAYES_00,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 29 Dec 2014 06:27:15 +0000 Received: from svr-orw-fem-06.mgc.mentorg.com ([147.34.97.120]) by relay1.mentorg.com with esmtp id 1Y5TnK-0002F2-7r from Yao_Qi@mentor.com ; Sun, 28 Dec 2014 22:27:10 -0800 Received: from GreenOnly (147.34.91.1) by SVR-ORW-FEM-06.mgc.mentorg.com (147.34.97.120) with Microsoft SMTP Server id 14.3.224.2; Sun, 28 Dec 2014 22:27:09 -0800 From: Yao Qi To: Doug Evans CC: Joel Brobecker , gdb-patches Subject: Re: [PATCH] Clear upper bits during sign extension References: <1419815569-21854-1-git-send-email-yao@codesourcery.com> <20141229030659.GB2123@adacore.com> <87d273w2gl.fsf@codesourcery.com> Date: Mon, 29 Dec 2014 06:27:00 -0000 In-Reply-To: (Doug Evans's message of "Sun, 28 Dec 2014 21:29:22 -0800") Message-ID: <878uhrvumr.fsf@codesourcery.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-IsSubscribed: yes X-SW-Source: 2014-12/txt/msg00656.txt.bz2 Doug Evans writes: > It's not immediately clear to this reader that undefined behaviour is > avoided here. > E.g., what if sizeof (LONGEST) =3D=3D 8 && bit =3D=3D 64. How about returning VALUE simply in this case? like the patch below, --=20 Yao (=E9=BD=90=E5=B0=A7) gdb: 2014-12-29 Yao Qi * utils.c (gdb_sign_extend): Do sign extension if BIT is less than 8 * sizeof (LONGEST). Clear bits from BIT in VALUE. diff --git a/gdb/utils.c b/gdb/utils.c index 47adb67..83a6df6 100644 --- a/gdb/utils.c +++ b/gdb/utils.c @@ -3028,10 +3028,13 @@ gdb_sign_extend (LONGEST value, int bit) { gdb_assert (bit >=3D 1 && bit <=3D 8 * sizeof (LONGEST)); =20 - if (((value >> (bit - 1)) & 1) !=3D 0) + if (bit < 8 * sizeof (LONGEST) && ((value >> (bit - 1)) & 1) !=3D 0) { LONGEST signbit =3D ((LONGEST) 1) << (bit - 1); =20 + /* Clear upper bits from bit BIT. */ + value &=3D (signbit << 1) - 1; + value =3D (value ^ signbit) - signbit; } =20