From patchwork Wed Dec 22 23:24:33 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 76457 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 1EB24B70DF for ; Thu, 23 Dec 2010 10:24:49 +1100 (EST) Received: (qmail 18461 invoked by alias); 22 Dec 2010 23:24:47 -0000 Received: (qmail 18451 invoked by uid 22791); 22 Dec 2010 23:24:45 -0000 X-SWARE-Spam-Status: No, hits=-2.9 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, SPF_HELO_PASS, T_RP_MATCHES_RCVD, T_TVD_MIME_NO_HEADERS X-Spam-Check-By: sourceware.org Received: from smtp-out.google.com (HELO smtp-out.google.com) (74.125.121.67) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 22 Dec 2010 23:24:40 +0000 Received: from wpaz24.hot.corp.google.com (wpaz24.hot.corp.google.com [172.24.198.88]) by smtp-out.google.com with ESMTP id oBMNObex024083 for ; Wed, 22 Dec 2010 15:24:37 -0800 Received: from pwj5 (pwj5.prod.google.com [10.241.219.69]) by wpaz24.hot.corp.google.com with ESMTP id oBMNOMcm009769 for ; Wed, 22 Dec 2010 15:24:35 -0800 Received: by pwj5 with SMTP id 5so402210pwj.29 for ; Wed, 22 Dec 2010 15:24:35 -0800 (PST) Received: by 10.142.217.18 with SMTP id p18mr6061788wfg.128.1293060275438; Wed, 22 Dec 2010 15:24:35 -0800 (PST) Received: from coign.google.com (dhcp-172-22-121-194.mtv.corp.google.com [172.22.121.194]) by mx.google.com with ESMTPS id w14sm9790965wfd.18.2010.12.22.15.24.34 (version=TLSv1/SSLv3 cipher=RC4-MD5); Wed, 22 Dec 2010 15:24:34 -0800 (PST) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: Go patch committed: Don't crash on index into erroneous map Date: Wed, 22 Dec 2010 15:24:33 -0800 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 X-System-Of-Record: true X-IsSubscribed: yes Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org This patch to the Go frontend avoids a crash when indexing into an erroneous map. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline. Ian diff -r 1fd3326a792f go/expressions.cc --- a/go/expressions.cc Wed Dec 22 15:08:11 2010 -0800 +++ b/go/expressions.cc Wed Dec 22 15:20:36 2010 -0800 @@ -9630,7 +9630,8 @@ Map_index_expression::get_map_type() const { Map_type* mt = this->map_->type()->deref()->map_type(); - gcc_assert(mt != NULL); + if (mt == NULL) + gcc_assert(saw_errors()); return mt; } @@ -9649,7 +9650,10 @@ Type* Map_index_expression::do_type() { - Type* type = this->get_map_type()->val_type(); + Map_type* mt = this->get_map_type(); + if (mt == NULL) + return Type::make_error_type(); + Type* type = mt->val_type(); // If this map index is in a tuple assignment, we actually return a // pointer to the value type. Tuple_map_assignment_statement is // responsible for handling this correctly. We need to get the type @@ -9665,7 +9669,9 @@ Map_index_expression::do_determine_type(const Type_context*) { this->map_->determine_type_no_context(); - Type_context subcontext(this->get_map_type()->key_type(), false); + Map_type* mt = this->get_map_type(); + Type* key_type = mt == NULL ? NULL : mt->key_type(); + Type_context subcontext(key_type, false); this->index_->determine_type(&subcontext); } @@ -9675,8 +9681,10 @@ Map_index_expression::do_check_types(Gogo*) { std::string reason; - if (!Type::are_assignable(this->get_map_type()->key_type(), - this->index_->type(), &reason)) + Map_type* mt = this->get_map_type(); + if (mt == NULL) + return; + if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason)) { if (reason.empty()) this->report_error(_("incompatible type for map index")); @@ -9695,6 +9703,8 @@ Map_index_expression::do_get_tree(Translate_context* context) { Map_type* type = this->get_map_type(); + if (type == NULL) + return error_mark_node; tree valptr = this->get_value_pointer(context, this->is_lvalue_); if (valptr == error_mark_node) @@ -9732,6 +9742,8 @@ bool insert) { Map_type* type = this->get_map_type(); + if (type == NULL) + return error_mark_node; tree map_tree = this->map_->get_tree(context); tree index_tree = this->index_->get_tree(context); diff -r 1fd3326a792f go/gogo.cc --- a/go/gogo.cc Wed Dec 22 15:08:11 2010 -0800 +++ b/go/gogo.cc Wed Dec 22 15:20:36 2010 -0800 @@ -3160,7 +3160,12 @@ Variable::type_from_tuple(Expression* expr, bool report_error) const { if (expr->map_index_expression() != NULL) - return expr->map_index_expression()->get_map_type()->val_type(); + { + Map_type* mt = expr->map_index_expression()->get_map_type(); + if (mt == NULL) + return Type::make_error_type(); + return mt->val_type(); + } else if (expr->receive_expression() != NULL) { Expression* channel = expr->receive_expression()->channel(); diff -r 1fd3326a792f go/statements.cc --- a/go/statements.cc Wed Dec 22 15:08:11 2010 -0800 +++ b/go/statements.cc Wed Dec 22 15:20:36 2010 -0800 @@ -922,6 +922,8 @@ return Statement::make_error_statement(loc); } Map_type* map_type = map_index->get_map_type(); + if (map_type == NULL) + return Statement::make_error_statement(loc); Block* b = new Block(enclosing, loc); @@ -1066,6 +1068,8 @@ return Statement::make_error_statement(loc); } Map_type* map_type = map_index->get_map_type(); + if (map_type == NULL) + return Statement::make_error_statement(loc); Block* b = new Block(enclosing, loc);