From patchwork Sun Mar 10 01:46:00 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 226405 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 7D7802C0320 for ; Sun, 10 Mar 2013 12:46:18 +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=1363484779; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Message-ID:Date:From:User-Agent:MIME-Version: To:CC:Subject:Content-Type:Mailing-List:Precedence:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=XV8RSpnwsYZlx7sedhMu9Qz0Dzw=; b=QbEjLoTkTzdrvJ4 D0HuzrQqM2bSU3VD+NgSNgOuSHi6QPD+AuA4yxeU0kRBAeWL+GvHilzcXiRFzaMQ o1P3nui2ZdZm/l8bwxxOgpXGRqdn67QlIQg6MANfx0OQ4yCaX1iFfWUKvdFItIQr /fPRyjnhq5Z1BqXQVrI0+Fx1BjJI= 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:Received:Received:Received:Message-ID:Date:From:User-Agent:MIME-Version:To:CC:Subject:Content-Type:X-IsSubscribed:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=epw1toha9rPGs7VOMMayUsK0/OavMgYBrsjUSm5I0dQzqHRY7lLZKVrh5l9B50 IN+uCa4sIZ3ENqoPiFtl8RjzqE3gJuHpJk+Z/3rSU4xGH2JX1EBrZeP+f9Mqia0J uKY7ZpoK2KR6STHNphjYPOvgWusFxWjzc4swLDqa7wiic=; Received: (qmail 8153 invoked by alias); 10 Mar 2013 01:46:11 -0000 Received: (qmail 8145 invoked by uid 22791); 10 Mar 2013 01:46:11 -0000 X-SWARE-Spam-Status: No, hits=-4.7 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_NO, RCVD_IN_HOSTKARMA_YE, RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from userp1040.oracle.com (HELO userp1040.oracle.com) (156.151.31.81) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sun, 10 Mar 2013 01:46:05 +0000 Received: from ucsinet22.oracle.com (ucsinet22.oracle.com [156.151.31.94]) by userp1040.oracle.com (Sentrion-MTA-4.3.1/Sentrion-MTA-4.3.1) with ESMTP id r2A1k4Pr012615 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Sun, 10 Mar 2013 01:46:05 GMT Received: from acsmt356.oracle.com (acsmt356.oracle.com [141.146.40.156]) by ucsinet22.oracle.com (8.14.4+Sun/8.14.4) with ESMTP id r2A1k3j6009951 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 10 Mar 2013 01:46:04 GMT Received: from abhmt114.oracle.com (abhmt114.oracle.com [141.146.116.66]) by acsmt356.oracle.com (8.12.11.20060308/8.12.11) with ESMTP id r2A1k33B021659; Sat, 9 Mar 2013 19:46:03 -0600 Received: from [192.168.1.4] (/79.33.221.193) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sat, 09 Mar 2013 17:46:02 -0800 Message-ID: <513BE5D8.9080202@oracle.com> Date: Sun, 10 Mar 2013 02:46:00 +0100 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130215 Thunderbird/17.0.3 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: Jason Merrill Subject: [C++ Patch] PR 56582 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 Hi, in cxx_eval_array_reference we don't check whether the subscript is negative and we end up either ICEing or emitting wrong code. Adding the check seems trivial, but I'm not sure if there is something more subtle to the issue which I'm missing. Also note that in principle we could have somewhat neater code but, as far as I can see, we would end up doing more comparisons (see the _alt patch). Tested x86_64-linux. Thanks, Paolo. //////////////////// /cp 2013-03-09 Paolo Carlini PR c++/56582 * semantics.c (cxx_eval_array_reference): Check for negative index. /testsuite 2013-03-09 Paolo Carlini PR c++/56582 * g++.dg/cpp0x/constexpr-array5.C: New. Index: cp/semantics.c =================================================================== --- cp/semantics.c (revision 196574) +++ cp/semantics.c (working copy) @@ -6990,23 +6990,23 @@ cxx_eval_array_reference (const constexpr_call *ca VERIFY_CONSTANT (ary); gcc_unreachable (); } - if (compare_tree_int (index, len) >= 0) + if (tree_int_cst_lt (index, integer_zero_node) + || !tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary)))) { - if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary)))) - { - /* If it's within the array bounds but doesn't have an explicit - initializer, it's value-initialized. */ - tree val = build_value_init (elem_type, tf_warning_or_error); - return cxx_eval_constant_expression (call, val, - allow_non_constant, addr, - non_constant_p, overflow_p); - } - if (!allow_non_constant) error ("array subscript out of bound"); *non_constant_p = true; return t; } + if (compare_tree_int (index, len) >= 0) + { + /* If it's within the array bounds but doesn't have an explicit + initializer, it's value-initialized. */ + tree val = build_value_init (elem_type, tf_warning_or_error); + return cxx_eval_constant_expression (call, val, + allow_non_constant, addr, + non_constant_p, overflow_p); + } i = tree_low_cst (index, 0); if (TREE_CODE (ary) == CONSTRUCTOR) return (*CONSTRUCTOR_ELTS (ary))[i].value; Index: testsuite/g++.dg/cpp0x/constexpr-array5.C =================================================================== --- testsuite/g++.dg/cpp0x/constexpr-array5.C (revision 0) +++ testsuite/g++.dg/cpp0x/constexpr-array5.C (working copy) @@ -0,0 +1,9 @@ +// PR c++/56582 +// { dg-do compile { target c++11 } } + +// Reliable ICE +constexpr int n[3] = {}; +constexpr int k = n[-1]; // { dg-error "out of bound" } + +// Some random byte +constexpr char c = "foo"[-1000]; // { dg-error "out of bound" } Index: cp/semantics.c =================================================================== --- cp/semantics.c (revision 196574) +++ cp/semantics.c (working copy) @@ -7007,6 +7007,13 @@ cxx_eval_array_reference (const constexpr_call *ca *non_constant_p = true; return t; } + else if (tree_int_cst_lt (index, integer_zero_node)) + { + if (!allow_non_constant) + error ("negative array subscript"); + *non_constant_p = true; + return t; + } i = tree_low_cst (index, 0); if (TREE_CODE (ary) == CONSTRUCTOR) return (*CONSTRUCTOR_ELTS (ary))[i].value; Index: testsuite/g++.dg/cpp0x/constexpr-array5.C =================================================================== --- testsuite/g++.dg/cpp0x/constexpr-array5.C (revision 0) +++ testsuite/g++.dg/cpp0x/constexpr-array5.C (working copy) @@ -0,0 +1,9 @@ +// PR c++/56582 +// { dg-do compile { target c++11 } } + +// Reliable ICE +constexpr int n[3] = {}; +constexpr int k = n[-1]; // { dg-error "negative" } + +// Some random byte +constexpr char c = "foo"[-1000]; // { dg-error "negative" }