From patchwork Sat Jul 3 06:05:00 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: 57792 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 53EBE1007D4 for ; Sat, 3 Jul 2010 16:05:17 +1000 (EST) Received: (qmail 24112 invoked by alias); 3 Jul 2010 06:05:15 -0000 Received: (qmail 23836 invoked by uid 22791); 3 Jul 2010 06:05:14 -0000 X-SWARE-Spam-Status: No, hits=-2.1 required=5.0 tests=AWL, BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, SPF_HELO_PASS, TW_CC, TW_FN, 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) (216.239.44.51) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 03 Jul 2010 06:05:09 +0000 Received: from hpaq7.eem.corp.google.com (hpaq7.eem.corp.google.com [172.25.149.7]) by smtp-out.google.com with ESMTP id o63657rR031761 for ; Fri, 2 Jul 2010 23:05:07 -0700 Received: from pxi12 (pxi12.prod.google.com [10.243.27.12]) by hpaq7.eem.corp.google.com with ESMTP id o636559V031751 for ; Fri, 2 Jul 2010 23:05:06 -0700 Received: by pxi12 with SMTP id 12so1721877pxi.6 for ; Fri, 02 Jul 2010 23:05:05 -0700 (PDT) Received: by 10.142.148.10 with SMTP id v10mr2266133wfd.106.1278137104907; Fri, 02 Jul 2010 23:05:04 -0700 (PDT) Received: from coign.google.com (adsl-71-133-8-30.dsl.pltn13.pacbell.net [71.133.8.30]) by mx.google.com with ESMTPS id f2sm1720985wfp.23.2010.07.02.23.05.02 (version=TLSv1/SSLv3 cipher=RC4-MD5); Fri, 02 Jul 2010 23:05:03 -0700 (PDT) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: [gccgo] Don't crash on argument count mismatch Date: Fri, 02 Jul 2010 23:05:00 -0700 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 Code like a, b := f() would crash the compiler when f returns a single result, rather than issuing a sensible error message. This patch fixes that. Committed to gccgo branch. Ian diff -r 19486205521b go/expressions.cc --- a/go/expressions.cc Fri Jul 02 12:59:20 2010 -0700 +++ b/go/expressions.cc Fri Jul 02 22:34:05 2010 -0700 @@ -8614,6 +8614,9 @@ void do_determine_type(const Type_context*); + void + do_check_types(Gogo*); + Expression* do_copy() { @@ -8661,23 +8664,50 @@ Type* Call_result_expression::do_type() { - if (this->call_->is_error_expression()) + // THIS->CALL_ can be replaced with a temporary reference due to + // Call_expression::do_must_eval_in_order when there is an error. + Call_expression* ce = this->call_->call_expression(); + if (ce == NULL) return Type::make_error_type(); - Function_type* fntype = - this->call_->call_expression()->get_function_type(); + Function_type* fntype = ce->get_function_type(); if (fntype == NULL) return Type::make_error_type(); const Typed_identifier_list* results = fntype->results(); Typed_identifier_list::const_iterator pr = results->begin(); for (unsigned int i = 0; i < this->index_; ++i) { - gcc_assert(pr != results->end()); + if (pr == results->end()) + return Type::make_error_type(); ++pr; } - gcc_assert(pr != results->end()); + if (pr == results->end()) + return Type::make_error_type(); return pr->type(); } +// Check the type. This is where we give an error if we're trying to +// extract too many values from a call. + +void +Call_result_expression::do_check_types(Gogo*) +{ + bool ok = true; + Call_expression* ce = this->call_->call_expression(); + if (ce != NULL) + ok = this->index_ < ce->result_count(); + else + { + // This can happen when the call returns a single value but we + // are asking for the second result. + if (this->call_->is_error_expression()) + return; + ok = false; + } + if (!ok) + error_at(this->location(), + "number of results does not match number of values"); +} + // Determine the type. We have nothing to do here, but the 0 result // needs to pass down to the caller. diff -r 19486205521b go/statements.cc --- a/go/statements.cc Fri Jul 02 12:59:20 2010 -0700 +++ b/go/statements.cc Fri Jul 02 22:34:05 2010 -0700 @@ -358,7 +358,10 @@ gcc_assert(this->decl_ == NULL_TREE); tree type_tree = this->type()->get_tree(context->gogo()); if (type_tree == error_mark_node) - return error_mark_node; + { + this->decl_ = error_mark_node; + return error_mark_node; + } // We can only use create_tmp_var if the type is not addressable. if (!TREE_ADDRESSABLE(type_tree)) {