From patchwork Fri May 11 21:09:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 912210 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-477603-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="xne6i97n"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 40jN5W2zL2z9s01 for ; Sat, 12 May 2018 07:09:49 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=u+umgF6zRrvN1ruG7ntbHJMiiGPPde83vid2KG2v/hXCkV TDqQTYcxi23GVSU96XOFYXzZ2kPKPD1E3u3uF/o4awiWMQZNQknkZOMJOm2x4ap/ NWoTE63uTcmjf9lgzyymnVqKelbmxTsv+AbOZ0Y6S0UEkEeQG6qEEXfHkmVSo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=WBqMzzY846wlAWzXWM4WcgbLb7Q=; b=xne6i97n9cmPS4hi6LdT aHYhWkmuT7LJQzvvOk/BexYj+0ciVhso2ushrbG87bsbXhto7yP8MJm9wh4l9fxW YfOHarppEL1SE9FnMPZ4CGmGnrMy5WBGlrN4u/68ArWuhl2ktrGH9NMfQJyl1GST KrTbaZqrIKXtSb+VinxSP3o= Received: (qmail 50046 invoked by alias); 11 May 2018 21:09:42 -0000 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 Received: (qmail 50036 invoked by uid 89); 11 May 2018 21:09:42 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 spammy=ccommonh, c-common.h, ccommonc, c-common.c X-HELO: mail-ot0-f177.google.com Received: from mail-ot0-f177.google.com (HELO mail-ot0-f177.google.com) (74.125.82.177) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 11 May 2018 21:09:39 +0000 Received: by mail-ot0-f177.google.com with SMTP id t1-v6so7741683ott.13 for ; Fri, 11 May 2018 14:09:39 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=TvK3MumFzn+qWT6g4VNwuD2HmTerYZHz88pcucTjqLw=; b=ca7xJgY205U+x4rXp1EW4YywoEs8RqO/lrZEbAfiz24sCSUy8Cnipygpl7LhIJBeRf nBq7L/xGGkjCMI/JNRgM3eTwpjYMKVltDI5f/X4XWBH/Ud+onbjr4TWuD/EihjFQRfM+ VjI2iuzMXdijJ4fqkVO/o0MCiGgzH0Io45yb3cwQf1HEnMwhoaKWFd1TzEon2lSsuOyn X8qI2g4oVnhWXXQK1LCZMsxy5kzL2ZAVlnrDvCkR20hV8g+zUC8StrgDdinrQYUrUHq6 HIeodpiZw/RTLIcRSuVm34x9UdYizb7MRWbOMRqheFIHVGydqK54mQM23krX4rk/KtWW lgLg== X-Gm-Message-State: ALKqPwcEMlWMiby8sPDj8XluYi63mvosELad7AcLPobhAnl/h7mFipoZ 0xb3iDiFmgaqprE7a8zBM/fz3w8HtCJGVdI17Pego2W1 X-Google-Smtp-Source: AB8JxZqoCYUapifOmdyYg34uGoR8+0g+JbKQgRmTvfSFszjoJ5t7pAD4ZseTrRtdi4frIEtkkfYtDRKOaftv+0RVVt8= X-Received: by 2002:a9d:e8f:: with SMTP id 15-v6mr5393582otj.14.1526072977228; Fri, 11 May 2018 14:09:37 -0700 (PDT) MIME-Version: 1.0 Received: by 10.201.13.229 with HTTP; Fri, 11 May 2018 14:09:16 -0700 (PDT) From: Jason Merrill Date: Fri, 11 May 2018 17:09:16 -0400 Message-ID: Subject: C++ PATCH to oversized array handling To: gcc-patches List X-IsSubscribed: yes We weren't checking for arrays being too large in tsubst, which meant that we don't catch that problem as early as we would like. Fixed by calling valid_array_size_p from tsubst. Tested x86_64-pc-linux-gnu, applying to trunk. commit eba9af072d35ec8cc0840622e30a096401d306fb Author: Jason Merrill Date: Fri Mar 16 11:55:54 2018 -0400 * pt.c (tsubst) [ARRAY_TYPE]: Check valid_array_size_p. (tsubst_copy_and_build) [NEW_EXPR]: Clear in_decl. gcc/c-family/ * c-common.c (valid_array_size_p): Add complain parameter. * c-common.h: ...which defaults to true. diff --git a/gcc/c-family/c-common.c b/gcc/c-family/c-common.c index e8bb3eb23f8..859eeb47252 100644 --- a/gcc/c-family/c-common.c +++ b/gcc/c-family/c-common.c @@ -7872,17 +7872,20 @@ reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */) the name of the array, or NULL_TREE for unnamed arrays. */ bool -valid_array_size_p (location_t loc, tree type, tree name) +valid_array_size_p (location_t loc, tree type, tree name, bool complain) { if (type != error_mark_node && COMPLETE_TYPE_P (type) && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST && !valid_constant_size_p (TYPE_SIZE_UNIT (type))) { - if (name) - error_at (loc, "size of array %qE is too large", name); - else - error_at (loc, "size of unnamed array is too large"); + if (complain) + { + if (name) + error_at (loc, "size of array %qE is too large", name); + else + error_at (loc, "size of unnamed array is too large"); + } return false; } return true; diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h index f2c66628e53..c266fee74c7 100644 --- a/gcc/c-family/c-common.h +++ b/gcc/c-family/c-common.h @@ -1248,7 +1248,7 @@ extern tree find_inv_trees (tree *, int *, void *); extern tree replace_inv_trees (tree *, int *, void *); extern bool reject_gcc_builtin (const_tree, location_t = UNKNOWN_LOCATION); -extern bool valid_array_size_p (location_t, tree, tree); +extern bool valid_array_size_p (location_t, tree, tree, bool = true); /* In c-warn.c. */ extern void constant_expression_warning (tree); diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 790d6ea25e9..cd9011b1b61 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -14788,6 +14788,10 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) r = build_cplus_array_type (type, domain); + if (!valid_array_size_p (input_location, r, in_decl, + (complain & tf_error))) + return error_mark_node; + if (TYPE_USER_ALIGN (t)) { SET_TYPE_ALIGN (r, TYPE_ALIGN (t)); @@ -18147,6 +18151,9 @@ tsubst_copy_and_build (tree t, } } + /* Avoid passing an enclosing decl to valid_array_size_p. */ + in_decl = NULL_TREE; + tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl); tree op2 = RECUR (TREE_OPERAND (t, 2)); ret = build_new (&placement_vec, op1, op2, &init_vec, diff --git a/gcc/testsuite/g++.dg/init/new38.C b/gcc/testsuite/g++.dg/init/new38.C index 37da525bf78..a03e231cbfe 100644 --- a/gcc/testsuite/g++.dg/init/new38.C +++ b/gcc/testsuite/g++.dg/init/new38.C @@ -22,7 +22,7 @@ template void large_array_template1(int n) { - new T[n] // { dg-error "size of array is too large" } + new T[n] // { dg-error "size of unnamed array is too large" } [(1ULL << (sizeof(void *) * 4)) / sizeof(T)] [1ULL << (sizeof(void *) * 4)]; } @@ -31,7 +31,7 @@ template void large_array_template2(int n) { - new T[n] // { dg-error "size of array is too large" } + new T[n] // { dg-error "size of unnamed array is too large" } [(1ULL << (sizeof(void *) * 4)) / sizeof(T)] [1ULL << (sizeof(void *) * 4)]; } @@ -40,7 +40,7 @@ template void large_array_template3(int n) { - new T[n] // { dg-error "size of array is too large" } + new T[n] // { dg-error "size of unnamed array is too large" } [(1ULL << (sizeof(void *) * 4)) / sizeof(T)] [1ULL << (sizeof(void *) * 4)]; } diff --git a/gcc/testsuite/g++.dg/template/array30.C b/gcc/testsuite/g++.dg/template/array30.C new file mode 100644 index 00000000000..3608c2e1233 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/array30.C @@ -0,0 +1,7 @@ +template +struct A +{ + int ar[I][I][I][I][I][I][I][I][I][I]; // { dg-error "too large" } +}; + +A<66000> a;