From patchwork Mon Dec 29 06:26:52 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yao Qi X-Patchwork-Id: 4450 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-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@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 14:26:52 +0800 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 X-IsSubscribed: yes Doug Evans writes: > It's not immediately clear to this reader that undefined behaviour is > avoided here. > E.g., what if sizeof (LONGEST) == 8 && bit == 64. How about returning VALUE simply in this case? like the patch below, 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 >= 1 && bit <= 8 * sizeof (LONGEST)); - if (((value >> (bit - 1)) & 1) != 0) + if (bit < 8 * sizeof (LONGEST) && ((value >> (bit - 1)) & 1) != 0) { LONGEST signbit = ((LONGEST) 1) << (bit - 1); + /* Clear upper bits from bit BIT. */ + value &= (signbit << 1) - 1; + value = (value ^ signbit) - signbit; }