From patchwork Tue Oct 2 04:44:19 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 29617 Received: (qmail 120062 invoked by alias); 2 Oct 2018 04:44: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 119825 invoked by uid 89); 2 Oct 2018 04:44:31 -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=UD:r, nicely, pr X-HELO: gateway32.websitewelcome.com Received: from gateway32.websitewelcome.com (HELO gateway32.websitewelcome.com) (192.185.145.102) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 02 Oct 2018 04:44:26 +0000 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway32.websitewelcome.com (Postfix) with ESMTP id 1F35F39F96 for ; Mon, 1 Oct 2018 23:44:25 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id 7CXpg7H3wkBj67CXpgEbSo; Mon, 01 Oct 2018 23:44:25 -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=ruLVoPMZlhZ3mNagOFfypK0o7+e1xZ/it4mB1jlBadQ=; b=JmMkUcQE1V3Xr6pJpbap10+KFy KljFH7Hv6kVDuW3e2nnSWmsM6VkFbcLehpAaEXqSZhXOXFh6PpmZZr6o2/fsgt+SFYfPtgdRdW6xo 8TD7QRUXnS9EoLSAj/1hzOH6z; 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-T7; Mon, 01 Oct 2018 23:44:24 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 09/10] Avoid undefined behavior in expression dumping Date: Mon, 1 Oct 2018 22:44:19 -0600 Message-Id: <20181002044420.17628-10-tom@tromey.com> In-Reply-To: <20181002044420.17628-1-tom@tromey.com> References: <20181002044420.17628-1-tom@tromey.com> -fsanitize=undefined pointed out undefined behavior in dump_raw_expression like: runtime error: load of value 2887952, which is not a valid value for type 'exp_opcode' dump_raw_expression will try to print the opcode for each element of the expression, even when it is not valid. To allow this, but have it avoid undefined behavior, this patch sets the underlying type of enum exp_opcode, and arranges for op_name to handle invalid opcodes more nicely. Before this patch, debug-expr.exp shows: Dump of expression @ 0x60f000007750, before conversion to prefix form: Language c, 8 elements, 16 bytes each. Index Opcode Hex Value String Value 0 OP_TYPE 89 Y............... 107820862850704 ..:..b.......... 2 OP_TYPE 89 Y............... 3 OP_VAR_VALUE 40 (............... 4 107820861806352 ..*..b.......... 5 107820861805152 `.*..b.......... 6 OP_VAR_VALUE 40 (............... 7 UNOP_MEMVAL_TYPE 57 9............... Afterward, the output is: Dump of expression @ 0x4820f90, before conversion to prefix form: Language c, 8 elements, 16 bytes each. Index Opcode Hex Value String Value 0 OP_TYPE 89 Y............... 1 unknown opcode: 176 75444400 .0.............. 2 OP_TYPE 89 Y............... 3 OP_VAR_VALUE 40 (............... 4 OP_BOOL 74616912 P.r............. 5 unknown opcode: 128 74615680 ..r............. 6 OP_VAR_VALUE 40 (............... 7 UNOP_MEMVAL_TYPE 57 9............... gdb/ChangeLog 2018-10-01 Tom Tromey * expression.h (enum exp_opcode): Use uint8_t as base type. * expprint.c (op_name): Handle invalid opcodes. --- gdb/ChangeLog | 5 +++++ gdb/expprint.c | 7 +++++++ gdb/expression.h | 2 +- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/gdb/expprint.c b/gdb/expprint.c index d6ed41253e..e87b3b709b 100644 --- a/gdb/expprint.c +++ b/gdb/expprint.c @@ -687,6 +687,13 @@ static int dump_subexp_body (struct expression *exp, struct ui_file *, int); const char * op_name (struct expression *exp, enum exp_opcode opcode) { + if (opcode >= OP_UNUSED_LAST) + { + char *cell = get_print_cell (); + xsnprintf (cell, PRINT_CELL_SIZE, "unknown opcode: %u", + unsigned (opcode)); + return cell; + } return exp->language_defn->la_exp_desc->op_name (opcode); } diff --git a/gdb/expression.h b/gdb/expression.h index bc7625f984..a5cb4c678e 100644 --- a/gdb/expression.h +++ b/gdb/expression.h @@ -39,7 +39,7 @@ and skip that many. Strings, like numbers, are indicated by the preceding opcode. */ -enum exp_opcode +enum exp_opcode : uint8_t { #define OP(name) name ,