From patchwork Wed Dec 5 00:23:16 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ian Lance Taylor X-Patchwork-Id: 203745 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 8FEF92C0085 for ; Wed, 5 Dec 2012 11:23:44 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1355271824; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:From:To:Subject:Date:Message-ID:User-Agent:MIME-Version: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=wNr2Z0i WpkWm2n9ATRAuMKN61ws=; b=jUYZUXXn2t02z5+pDvAKzGUq4N5ZbGRyQ+H+vZ8 6+sdYBCppTlVyO1aMrSGNBDjfifptPi0IPlUEwrvYOrz19QcCQ8wWChMs+RKOtZo fZnQg+VMBmcMJZBLG5BnwQitvjd4b7QqZ0orRMa5MJBM4dv8uNN3Nv8HyfsHVkYF nFoU= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:X-Google-DKIM-Signature:Received:Received:From:To:Subject:Date:Message-ID:User-Agent:MIME-Version:Content-Type:X-Gm-Message-State:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=Vh/cz8f2pSIibCGetmfJRkS6d09IC7f+DaVRpDXITsLCyxHBsdVSILc5UQQTon eIbFMxcvagE2Z4iFrarzUbJrRiS55YZL/UJKYecPqESmzEyT4+5fSkLgeurolVub aJWQwA9ddKQX0kCmwKRLwwWe0CC9k25Yd5lPixjboYmb8=; Received: (qmail 494 invoked by alias); 5 Dec 2012 00:23:34 -0000 Received: (qmail 432 invoked by uid 22791); 5 Dec 2012 00:23:29 -0000 X-SWARE-Spam-Status: No, hits=-4.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, KHOP_RCVD_TRUST, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD, T_TVD_MIME_NO_HEADERS X-Spam-Check-By: sourceware.org Received: from mail-da0-f47.google.com (HELO mail-da0-f47.google.com) (209.85.210.47) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 05 Dec 2012 00:23:23 +0000 Received: by mail-da0-f47.google.com with SMTP id s35so1803691dak.20 for ; Tue, 04 Dec 2012 16:23:22 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:subject:date:message-id:user-agent:mime-version :content-type:x-gm-message-state; bh=1b5CP7OzNfHwjhbACOyuV3clbuc0GVVWB5ZW8l+V0tU=; b=ZFTBhLjoNHl/bRQDbKXlWUyd7EVWdnLSSLRlzJnNm35pwjDwBlVqtAbWMrMHj1Vngy PymaEFSku9+lxWEFv3NLmzCOi5zuezmvgwEbfYBJz5H8WZi5ZDsBU6MU3p8brOVFiPM3 Qk/K1hgbCqLLOtPydLMLFM2puKi9tsnYU3MYSYfR7XOcRiZWocMxty7z48mY/oZ6RRli gbywxJw5mNtvQLry8nGrb/bElNPw3FDRd9YdBKMQzqCjYQ6eoU6vSA4G8UlhYxSTYySU kdTQi7A21dm7uLxmkt8R5Ts1G49w6/5qESYasZRRM0/vqn7cWvE8xdTTshb/fY+osXmP xm9g== Received: by 10.66.52.79 with SMTP id r15mr39512444pao.46.1354667002491; Tue, 04 Dec 2012 16:23:22 -0800 (PST) Received: from coign.google.com ([2401:fa00:0:9:224:d7ff:fe8f:f634]) by mx.google.com with ESMTPS id pm8sm546410pbb.29.2012.12.04.16.23.19 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 04 Dec 2012 16:23:21 -0800 (PST) From: Ian Lance Taylor To: gcc-patches@gcc.gnu.org, gofrontend-dev@googlegroups.com Subject: Go patch committed: Better checks for make arguments Date: Tue, 04 Dec 2012 16:23:16 -0800 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 X-Gm-Message-State: ALoCoQkT148BsPQ2Wj6HLpT/5uYty6kuIwCZ6F6QWbbA7/q4vakVs5Ceq+/KiBaNGzZXbrnKjiG1vvcActUasG95X06pt1lUgDuNua0tsW62HG36RBhzRUcBUjhl77M7PRmSIdUebst1nJ9FTdAdUgL3KfTcNF7roKz7ilb5Z28cBpRd32PQASTeT4HaDdFYKzmTBFRXYhGg 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 implements better checks for constants passed to the predefined function make. It gives an error for using negative numbers, and when the length is larger than the capacity. These are required by the language spec, but were never enforced by the compiler until now. This required updating one test case. Bootstrapped and ran Go testsuite on x86_64-unknown-linux-gnu. Committed to mainline. Ian Index: gcc/testsuite/go.test/test/fixedbugs/bug273.go =================================================================== --- gcc/testsuite/go.test/test/fixedbugs/bug273.go (revision 194172) +++ gcc/testsuite/go.test/test/fixedbugs/bug273.go (revision 194173) @@ -11,6 +11,7 @@ package main var bug = false var minus1 = -1 +var five = 5 var big int64 = 10 | 1<<32 type block [1<<19]byte @@ -40,7 +41,7 @@ func badcap() { } func badcap1() { - g1 = make([]block, 10, 5) + g1 = make([]block, 10, five) } func bigcap() { Index: gcc/go/gofrontend/expressions.cc =================================================================== --- gcc/go/gofrontend/expressions.cc (revision 194172) +++ gcc/go/gofrontend/expressions.cc (revision 194173) @@ -6623,7 +6623,7 @@ class Builtin_call_expression : public C lower_make(); bool - check_int_value(Expression*); + check_int_value(Expression*, bool is_length); // A pointer back to the general IR structure. This avoids a global // variable, or passing it around everywhere. @@ -6897,11 +6897,8 @@ Builtin_call_expression::lower_make() else { len_arg = *parg; - if (!this->check_int_value(len_arg)) - { - this->report_error(_("bad size for make")); - return Expression::make_error(this->location()); - } + if (!this->check_int_value(len_arg, true)) + return Expression::make_error(this->location()); if (len_arg->type()->integer_type() != NULL && len_arg->type()->integer_type()->bits() > uintptr_bits) have_big_args = true; @@ -6912,11 +6909,23 @@ Builtin_call_expression::lower_make() if (is_slice && parg != args->end()) { cap_arg = *parg; - if (!this->check_int_value(cap_arg)) + if (!this->check_int_value(cap_arg, false)) + return Expression::make_error(this->location()); + + Numeric_constant nclen; + Numeric_constant nccap; + unsigned long vlen; + unsigned long vcap; + if (len_arg->numeric_constant_value(&nclen) + && cap_arg->numeric_constant_value(&nccap) + && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID + && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID + && vlen > vcap) { - this->report_error(_("bad capacity when making slice")); + this->report_error(_("len larger than cap")); return Expression::make_error(this->location()); } + if (cap_arg->type()->integer_type() != NULL && cap_arg->type()->integer_type()->bits() > uintptr_bits) have_big_args = true; @@ -6973,20 +6982,36 @@ Builtin_call_expression::lower_make() // function. bool -Builtin_call_expression::check_int_value(Expression* e) +Builtin_call_expression::check_int_value(Expression* e, bool is_length) { - if (e->type()->integer_type() != NULL) - return true; - - // Check for a floating point constant with integer value. Numeric_constant nc; - mpz_t ival; - if (e->numeric_constant_value(&nc) && nc.to_int(&ival)) + if (e->numeric_constant_value(&nc)) { - mpz_clear(ival); - return true; + unsigned long v; + switch (nc.to_unsigned_long(&v)) + { + case Numeric_constant::NC_UL_VALID: + return true; + case Numeric_constant::NC_UL_NOTINT: + error_at(e->location(), "non-integer %s argument to make", + is_length ? "len" : "cap"); + return false; + case Numeric_constant::NC_UL_NEGATIVE: + error_at(e->location(), "negative %s argument to make", + is_length ? "len" : "cap"); + return false; + case Numeric_constant::NC_UL_BIG: + // We don't want to give a compile-time error for a 64-bit + // value on a 32-bit target. + return true; + } } + if (e->type()->integer_type() != NULL) + return true; + + error_at(e->location(), "non-integer %s argument to make", + is_length ? "len" : "cap"); return false; }