From patchwork Mon Aug 27 14:56:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 29068 Received: (qmail 64075 invoked by alias); 27 Aug 2018 14:57:34 -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 63301 invoked by uid 89); 27 Aug 2018 14:57:09 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-HELO: gateway21.websitewelcome.com Received: from gateway21.websitewelcome.com (HELO gateway21.websitewelcome.com) (192.185.46.113) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 27 Aug 2018 14:57:06 +0000 Received: from cm16.websitewelcome.com (cm16.websitewelcome.com [100.42.49.19]) by gateway21.websitewelcome.com (Postfix) with ESMTP id 56584400E4CB7 for ; Mon, 27 Aug 2018 09:56:55 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id uIwMf9EtOaSeyuIwefdeWz; Mon, 27 Aug 2018 09:56:54 -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=5Bx/jZ7LZSPVH5ZVE7Gce5ZTYnNvkA3M1/rQ7QyEm3w=; b=LSrdkZ0xMzAw7pV3Cc97C4Vsa1 IWsg1vx2vzJ4/cMbnWDI1Bz48qxZWIAFZS3DBZuTbeFcTBccvt9RctwB4rcPh+1gZHPPQglEoBfVa 6ZF/I2UNKs53UecTWK+QmEu7Z; Received: from 75-166-85-72.hlrn.qwest.net ([75.166.85.72]:54030 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1fuIwM-000csy-9g; Mon, 27 Aug 2018 09:56:26 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 5/9] Avoid undefined behavior in parse_number Date: Mon, 27 Aug 2018 08:56:16 -0600 Message-Id: <20180827145620.11055-6-tom@tromey.com> In-Reply-To: <20180827145620.11055-1-tom@tromey.com> References: <20180827145620.11055-1-tom@tromey.com> -fsanitize=undefined pointed out that c-exp.y relied on undefined behavior here: if (c != 'l' && c != 'u') n *= base; ...when a large hex constant "just fit" into a LONGEST, causing the high bit to be set. This fixes the problem by having the function work in an unsigned type. ChangeLog 2018-08-27 Tom Tromey * c-exp.y (parse_number): Work in unsigned. Remove casts. --- gdb/ChangeLog | 4 ++++ gdb/c-exp.y | 10 ++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/gdb/c-exp.y b/gdb/c-exp.y index a9ccbdcb65e..accee5d5451 100644 --- a/gdb/c-exp.y +++ b/gdb/c-exp.y @@ -1760,10 +1760,8 @@ static int parse_number (struct parser_state *par_state, const char *buf, int len, int parsed_float, YYSTYPE *putithere) { - /* FIXME: Shouldn't these be unsigned? We don't deal with negative values - here, and we do kind of silly things like cast to unsigned. */ - LONGEST n = 0; - LONGEST prevn = 0; + ULONGEST n = 0; + ULONGEST prevn = 0; ULONGEST un; int i = 0; @@ -1922,7 +1920,7 @@ parse_number (struct parser_state *par_state, on 0x123456789 when LONGEST is 32 bits. */ if (c != 'l' && c != 'u' && n != 0) { - if ((unsigned_p && (ULONGEST) prevn >= (ULONGEST) n)) + if (unsigned_p && prevn >= n) error (_("Numeric constant too large.")); } prevn = n; @@ -1940,7 +1938,7 @@ parse_number (struct parser_state *par_state, the case where it is we just always shift the value more than once, with fewer bits each time. */ - un = (ULONGEST)n >> 2; + un = n >> 2; if (long_p == 0 && (un >> (gdbarch_int_bit (parse_gdbarch (par_state)) - 2)) == 0) {