From patchwork Tue Oct 2 04:44: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: 29615 Received: (qmail 120177 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 119814 invoked by uid 89); 2 Oct 2018 04:44:28 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.4 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=Hx-languages-length:1656 X-HELO: gateway31.websitewelcome.com Received: from gateway31.websitewelcome.com (HELO gateway31.websitewelcome.com) (192.185.143.43) 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 gateway31.websitewelcome.com (Postfix) with ESMTP id 45C4F2AB08 for ; Mon, 1 Oct 2018 23:44:24 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 7CXog0nhAPvAd7CXogrlQl; 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=WndMHRangmFR1WAwVvGWolw0z5mPlEGJ53NXP6QCbcw=; b=eCZJOhH56LEZagK5+HMvksiOqP h8w++6QFwY+zUgDOIYTvn7sRUzeB6UCR/UNwdUbrPBygXMTxNNNVHz7Nafl72kBmroxbGTDjUg9Rc bs/MdXk82WloLPjFqRAorDjYs; 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-2S; Mon, 01 Oct 2018 23:44:24 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 06/10] Avoid undefined behavior in parse_number Date: Mon, 1 Oct 2018 22:44:16 -0600 Message-Id: <20181002044420.17628-7-tom@tromey.com> In-Reply-To: <20181002044420.17628-1-tom@tromey.com> References: <20181002044420.17628-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. gdb/ChangeLog 2018-10-01 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 0326ee090e..09e31d2283 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) {