From patchwork Thu Aug 30 02:44:15 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 29123 Received: (qmail 22285 invoked by alias); 30 Aug 2018 02:44:43 -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 21824 invoked by uid 89); 30 Aug 2018 02:44:40 -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=pr, indicated, debug-expr.exp, *exp X-HELO: gateway32.websitewelcome.com Received: from gateway32.websitewelcome.com (HELO gateway32.websitewelcome.com) (192.185.145.1) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 30 Aug 2018 02:44:37 +0000 Received: from cm10.websitewelcome.com (cm10.websitewelcome.com [100.42.49.4]) by gateway32.websitewelcome.com (Postfix) with ESMTP id 7D0CE58F826 for ; Wed, 29 Aug 2018 21:44:35 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id vCwXfj3WcBcCXvCwffD8ko; Wed, 29 Aug 2018 21:44:34 -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=+2E3Wu0E7Aca834qAt5YWjLZBBGPiPB0mWIvkLhiPZw=; b=M97wqtyvnbEtq4Oopv7ReP48BE 6csoi6s7py50m+5JGQFWbKW2dUmtyJJJgmfRmCb66qj8MihE70C2KssmwsDSKGHeE3TzdAuAGZqDH Q659qQMxeg8bbmkaGDD+q6KYg; 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-9d; Wed, 29 Aug 2018 21:44:21 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 09/10] Avoid undefined behavior in expression dumping Date: Wed, 29 Aug 2018 20:44:15 -0600 Message-Id: <20180830024416.23386-10-tom@tromey.com> In-Reply-To: <20180830024416.23386-1-tom@tromey.com> References: <20180830024416.23386-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-08-29 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 d6ed41253ed..e87b3b709b9 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 9f26bb8d60b..db572efe2a3 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 ,