From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 542 invoked by alias); 26 Jan 2016 23:02:53 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 521 invoked by uid 89); 26 Jan 2016 23:02:52 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Hx-languages-length:2149, vla X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Tue, 26 Jan 2016 23:02:51 +0000 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (Postfix) with ESMTPS id B496B8E925 for ; Tue, 26 Jan 2016 23:02:50 +0000 (UTC) Received: from redhat.com (ovpn-204-33.brq.redhat.com [10.40.204.33]) by int-mx14.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0QN2l66027385 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Tue, 26 Jan 2016 18:02:49 -0500 Date: Tue, 26 Jan 2016 23:02:00 -0000 From: Marek Polacek To: GCC Patches , Jason Merrill Subject: C++ PATCH for c++/69496 (ICE on VLA in constexpr function) Message-ID: <20160126230246.GO25528@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) X-SW-Source: 2016-01/txt/msg02057.txt.bz2 The (invalid) testcase was causing an ICE because we were passing the result of array_type_nelts_top immediately into tree_int_cst_lt, but for VLAs, the result doesn't have to be a constant. Fixed by evaluating the bound of the array so that we're able to give a proper out-of-bounds diagnostics. And the VERIFY_CONSTANT should ensure that if the array bound couldn't be reduced to a constant, we bail out rather than evoke an ICE. Bootstrapped/regtested on x86_64-linux, ok for trunk? 2016-01-26 Marek Polacek PR c++/69496 * constexpr.c (cxx_eval_array_reference): Evaluate the number of elements of the array. * g++.dg/cpp1y/constexpr-vla1.C: New test. diff --git gcc/cp/constexpr.c gcc/cp/constexpr.c index 6b0e5a8..6324e45 100644 --- gcc/cp/constexpr.c +++ gcc/cp/constexpr.c @@ -1855,7 +1855,12 @@ cxx_eval_array_reference (const constexpr_ctx *ctx, tree t, if (!found) { - if (tree_int_cst_lt (index, array_type_nelts_top (TREE_TYPE (ary)))) + tree nelts = array_type_nelts_top (TREE_TYPE (ary)); + /* For VLAs, the number of elements won't be an integer constant. */ + nelts = cxx_eval_constant_expression (ctx, nelts, false, non_constant_p, + overflow_p); + VERIFY_CONSTANT (nelts); + if (tree_int_cst_lt (index, nelts)) { if (TREE_CODE (ary) == CONSTRUCTOR && CONSTRUCTOR_NO_IMPLICIT_ZERO (ary)) diff --git gcc/testsuite/g++.dg/cpp1y/constexpr-vla1.C gcc/testsuite/g++.dg/cpp1y/constexpr-vla1.C index e69de29..a5615bb 100644 --- gcc/testsuite/g++.dg/cpp1y/constexpr-vla1.C +++ gcc/testsuite/g++.dg/cpp1y/constexpr-vla1.C @@ -0,0 +1,30 @@ +// PR c++/69496 +// { dg-do compile { target c++14 } } + +constexpr int +fn_ok (int n) +{ + __extension__ int a[n] = { }; + int z = 0; + + for (unsigned i = 0; i < sizeof (a) / sizeof (int); ++i) + z += a[i]; + + return z; +} + + +constexpr int +fn_not_ok (int n) +{ + __extension__ int a[n] = { }; + int z = 0; + + for (unsigned i = 0; i < sizeof (a); ++i) + z += a[i]; + + return z; +} + +constexpr int n1 = fn_ok (3); +constexpr int n2 = fn_not_ok (3); // { dg-error "array subscript out of bound" } Marek