From patchwork Tue Oct 2 04:44:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 29616 Received: (qmail 120249 invoked by alias); 2 Oct 2018 04:44:35 -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 119816 invoked by uid 89); 2 Oct 2018 04:44:28 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.0 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_NUMSUBJECT, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:1686 X-HELO: gateway30.websitewelcome.com Received: from gateway30.websitewelcome.com (HELO gateway30.websitewelcome.com) (192.185.146.7) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 02 Oct 2018 04:44:25 +0000 Received: from cm17.websitewelcome.com (cm17.websitewelcome.com [100.42.49.20]) by gateway30.websitewelcome.com (Postfix) with ESMTP id 8D9296449 for ; Mon, 1 Oct 2018 23:44:24 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 7CXog0nhMPvAd7CXogrlR2; Mon, 01 Oct 2018 23:44:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=Kyf90gWb3TKmxasJHUwm9dOABf+8FQs1ldjzAh/pK/Y=; b=tv5xVEVQF+S6ezxwSt1f5Qmesi sKI27CewUJ9+/IBKJTaVVPXaa7bGjJ9XeM1PPZqUdEHn3VIl3GMxyXbijfZ4OBTEzNBbxh80+wct1 fg3Z2rfrk8vpl3LGUsPB/ZAYd; Received: from 97-122-190-66.hlrn.qwest.net ([97.122.190.66]:32984 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1g7CXo-003mHT-BK; Mon, 01 Oct 2018 23:44:24 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 07/10] Avoid undefined behavior in read_signed_leb128 Date: Mon, 1 Oct 2018 22:44:17 -0600 Message-Id: <20181002044420.17628-8-tom@tromey.com> In-Reply-To: <20181002044420.17628-1-tom@tromey.com> References: <20181002044420.17628-1-tom@tromey.com> -fsanitize=undefined pointed out that read_signed_leb128 had an undefined left-shift when processing the final byte of a 64-bit leb: runtime error: left shift of 127 by 63 places cannot be represented in type 'long int' and an undefined negation: runtime error: negation of -9223372036854775808 cannot be represented in type 'long int'; cast to an unsigned type to negate this value to itself Both of these problems are readily avoided by havinng read_signed_leb128 work in an unsigned type, and then casting to the signed type at the return. gdb/ChangeLog 2018-10-01 Tom Tromey * dwarf2read.c (read_signed_leb128): Work in ULONGEST. --- gdb/ChangeLog | 4 ++++ gdb/dwarf2read.c | 6 +++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c index 4013c199da..7004299a91 100644 --- a/gdb/dwarf2read.c +++ b/gdb/dwarf2read.c @@ -19627,7 +19627,7 @@ static LONGEST read_signed_leb128 (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read_ptr) { - LONGEST result; + ULONGEST result; int shift, num_read; unsigned char byte; @@ -19639,7 +19639,7 @@ read_signed_leb128 (bfd *abfd, const gdb_byte *buf, byte = bfd_get_8 (abfd, buf); buf++; num_read++; - result |= ((LONGEST) (byte & 127) << shift); + result |= ((ULONGEST) (byte & 127) << shift); shift += 7; if ((byte & 128) == 0) { @@ -19647,7 +19647,7 @@ read_signed_leb128 (bfd *abfd, const gdb_byte *buf, } } if ((shift < 8 * sizeof (result)) && (byte & 0x40)) - result |= -(((LONGEST) 1) << shift); + result |= -(((ULONGEST) 1) << shift); *bytes_read_ptr = num_read; return result; }