From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19933 invoked by alias); 30 Aug 2018 02:44:24 -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 19791 invoked by uid 89); 30 Aug 2018 02:44:23 -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=exp_opcode, 2887952, parse.c, parsec X-HELO: gateway21.websitewelcome.com Received: from gateway21.websitewelcome.com (HELO gateway21.websitewelcome.com) (192.185.45.133) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 30 Aug 2018 02:44:22 +0000 Received: from cm13.websitewelcome.com (cm13.websitewelcome.com [100.42.49.6]) by gateway21.websitewelcome.com (Postfix) with ESMTP id 384D8400CECC4 for ; Wed, 29 Aug 2018 21:44:21 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id vCwXf2kdc79N3vCwXfH1mh; Wed, 29 Aug 2018 21:44:21 -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=trOv+nlzWqG48zxbHL/uXC4FiAGufXNxMwHNQjMrXXA=; b=rdGmQDAOHwMOF7vO1C/ZTxseh+ xMA5s+hvYKZAjL3Hy20jR7SgRO/7SoXM7cc9tF3BTHmYPdhaZ4Y8IOvJrC80X0ku5Cdm9CGNPL4Rh /egJU12uLAjdl5g3GWT8KlMwK; Received: from 75-166-85-72.hlrn.qwest.net ([75.166.85.72]:37418 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1fvCwX-000S4x-06; Wed, 29 Aug 2018 21:44:21 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 08/10] Avoid undefined behavior in ada_operator_length Date: Thu, 30 Aug 2018 02:44:00 -0000 Message-Id: <20180830024416.23386-9-tom@tromey.com> In-Reply-To: <20180830024416.23386-1-tom@tromey.com> References: <20180830024416.23386-1-tom@tromey.com> X-SW-Source: 2018-08/txt/msg00807.txt.bz2 -fsanitize=undefined pointed out this error: runtime error: load of value 2887952, which is not a valid value for type 'exp_opcode' This happens in gdb.ada/complete.exp when processing "complete p my_glob". This does not parse, so the Ada parser throws an exception; but then the code in parse_exp_in_context_1 accepts the expression anyway. However, as no elements have been written to the expression, undefined behavior results. The fix is to notice this case in parse_exp_in_context_1. This patch also adds an assertion to prefixify_expression to enforce this pre-existing constraint. gdb/ChangeLog 2018-08-29 Tom Tromey * parse.c (prefixify_expression): Add assert. (parse_exp_in_context_1): Throw exception if the expression is empty. --- gdb/ChangeLog | 8 +++++++- gdb/parse.c | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/gdb/parse.c b/gdb/parse.c index 163852cb84c..8a128a74d77 100644 --- a/gdb/parse.c +++ b/gdb/parse.c @@ -792,6 +792,7 @@ copy_name (struct stoken token) int prefixify_expression (struct expression *expr) { + gdb_assert (expr->nelts > 0); int len = sizeof (struct expression) + EXP_ELEM_TO_BYTES (expr->nelts); struct expression *temp; int inpos = expr->nelts, outpos = 0; @@ -1205,7 +1206,10 @@ parse_exp_in_context_1 (const char **stringptr, CORE_ADDR pc, } CATCH (except, RETURN_MASK_ALL) { - if (! parse_completion) + /* If parsing for completion, allow this to succeed; but if no + expression elements have been written, then there's nothing + to do, so fail. */ + if (! parse_completion || ps.expout_ptr == 0) throw_exception (except); } END_CATCH -- 2.13.6