From patchwork Sat Mar 17 12:13:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Oliva X-Patchwork-Id: 887305 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-474902-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="dgv9wqo0"; 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 403Lpd02Grz9s9h for ; Sat, 17 Mar 2018 23:14:00 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:mime-version:content-type; q=dns; s= default; b=oyu8M9LlPF9zX2Km5zs6NrckYb8/nzMzYQUc+DFeSWRDrtcryRZZR O2u4QRbuoe/lEWsaqHzMxjyqSPICO9QS1jMg1nc7p3tUd9RQ62svvLQeWGKRbmhU 2wHqxp8W81F3PLERA4rLNkqDPlZqAKVu1Sl7Cb4lSDzVb0tKAfN+LE= 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:from :to:subject:date:message-id:mime-version:content-type; s= default; bh=vVVKaOB7vPjbxE+4MC5Wl7MOw5o=; b=dgv9wqo0tpRN8k72+/yE r/ZS4f9KBEo6OOlOXkCspDJt9QRfXcoo10/kU++WlPpOt3OVJ82soMl5amsp0rBR XR4/B5/MYyLboUA5mtDjRJNfP7loen5r6CMAMdN6YEXmdrxrlVzK/o7e1MFaOKaN RxsX6WTj5QhZXOgn+ZKiFoY= Received: (qmail 103744 invoked by alias); 17 Mar 2018 12:13:54 -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 103735 invoked by uid 89); 17 Mar 2018 12:13:53 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_LAZY_DOMAIN_SECURITY, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= 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 ESMTP; Sat, 17 Mar 2018 12:13:51 +0000 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C16B085359 for ; Sat, 17 Mar 2018 12:13:50 +0000 (UTC) Received: from freie.home (ovpn04.gateway.prod.ext.phx2.redhat.com [10.5.9.4]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8419B5D734 for ; Sat, 17 Mar 2018 12:13:50 +0000 (UTC) Received: from livre (livre.home [172.31.160.2]) by freie.home (8.15.2/8.15.2) with ESMTP id w2HCDjJY167945; Sat, 17 Mar 2018 09:13:45 -0300 From: Alexandre Oliva To: gcc-patches@gcc.gnu.org Subject: [PR c++/71251] out-of-range parms in tmpl arg substitution Date: Sat, 17 Mar 2018 09:13:45 -0300 Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.3 (gnu/linux) MIME-Version: 1.0 As we go through each of the template parameters, substituting it with corresponding template arguments, an incorrect argument list might cause us to index argument vectors past their length (or fail in the preceding tree checks). Avoid such dereferences and instead issue an error (if requested) if we find the argument index to be past the parameter vector length. Regstrapped on i686- and x86_64-linux-gnu. Ok to install? for gcc/cp/ChangeLog PR c++/71251 * pt.c (tsubst): Test for and report out-of-range template parms. for gcc/testsuite/ChangeLog PR c++/71251 * g++.dg/cpp0x/pr71251.C: New. --- gcc/cp/pt.c | 27 +++++++++++++++++++++++---- gcc/testsuite/g++.dg/cpp0x/pr71251.C | 9 +++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/pr71251.C diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index fa9bfb12c297..4cc18d0abe78 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -13976,11 +13976,30 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl) if (level <= levels && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0) { - arg = TMPL_ARG (args, level, idx); + if (TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > idx) + { + arg = TMPL_ARG (args, level, idx); - /* See through ARGUMENT_PACK_SELECT arguments. */ - if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT) - arg = argument_pack_select_arg (arg); + /* See through ARGUMENT_PACK_SELECT arguments. */ + if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT) + arg = argument_pack_select_arg (arg); + } + else + { + if (complain & tf_error) + { + /* ??? We could use a better location for this + message. It takes the context of the closing + bracket of the innermost template we're + substituting, but the error may very well be + related with some enclosing context. */ + error ("missing template argument at this" + " or enclosing context"); + error_at (DECL_SOURCE_LOCATION (TEMPLATE_PARM_DECL (t)), + "for substitution of template parameter"); + } + arg = error_mark_node; + } } if (arg == error_mark_node) diff --git a/gcc/testsuite/g++.dg/cpp0x/pr71251.C b/gcc/testsuite/g++.dg/cpp0x/pr71251.C new file mode 100644 index 000000000000..f16fb13cccc0 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/pr71251.C @@ -0,0 +1,9 @@ +// { dg-do compile { target c++11 } } + +template template using U=void; // { dg-error "parameter" } + +template struct S1; + +template struct S1>{ template struct S2:S2{}; }; // { dg-error "missing" } +//^ the error should be here, but it is^here, thus the weird line break