From patchwork Fri Feb 9 18:32:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 199051 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp1050107dyd; Fri, 9 Feb 2024 10:33:12 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXU2aW2aoahebDKEgukGzTo9QaLfKaGhvVPMcrsiHKcpbwY8u3kucQ+6MzpHqx7/3mo+Rc4Tgmw7S4Rl8fmMv8xXB/+iA== X-Google-Smtp-Source: AGHT+IEgJp7A8jquaf2J8cTdJcfpYABomp7DNOlBUn+r1T8LtGcEQ/Kk6X7zZohyIodmifA4ieyy X-Received: by 2002:a05:620a:a14:b0:785:91bf:39b2 with SMTP id i20-20020a05620a0a1400b0078591bf39b2mr2333974qka.6.1707503592662; Fri, 09 Feb 2024 10:33:12 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707503592; cv=pass; d=google.com; s=arc-20160816; b=AA7mAB2GDVbaStIsoiDVJSKv+FMgtj8ifLmYqQNrbMv0O/Qqe+qaZoOxiFr5x7Cleb 1YhZzboMu+i/exnJO7S31xYbvl9No5m5UpadRDQq+7IhSiqKaUT8qINFLa5XgmuNx1Ow VdX9xf0W0/eXIRUrN/abWRkCRJLGi64aKae6tRvRsPS5/qNYaaNAWFoMMNU8pkGktkwZ Unl4rIUiSkIxnt9K0QMhtENCnqGQmd48TboRtXRH4tgY5LUU/sMXgathGXsL7K4tbQ8X lyPXif/oQzAYFBemvQ6ZF56IvC4op5Sfp3NfDfQnanWpjijVJoYjZDrMxd6+zBJ2dKpg 4nvg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=bpSHeHdhXafz1N33X/6tmA1+4rCWXuCbD7XSUUthjvQ=; fh=TSmORUuMwmQuJNTRBp6+AalTtuWrY9bktclHZ9CScTk=; b=DaScetVmICIBVUevMkUZ7zFFICCjFUtQm4WHbhElTVhUL32pZklFV4NHNS7jet844N 81DDdX6iGrFHA4A/0jIlil9EV5G2XYhaFr4XGjVTsEKf5x2vhaavkCHjB+ro3IsKAK0q Qq0vaQmCd2Yr1oFc+EntmbkG357E7+lCq/g8EWfqcziIlG4q4BilRsXCts3pGoXDnR3o 9dp/XJFIycLU/FyaPPOiXM9hQhTWOpp4AiQeq2IWB3JL+aIHg5dqQaRWTBmksIS7xhRa v7Zn6dL3eAwSOBUL4U9wKhJdx9EcwvLb1MYV81QvctP7vZgB5C/nZbQTxGTpOQyQcLyf 5S7Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JMXp6bhd; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com X-Forwarded-Encrypted: i=2; AJvYcCXkzirnkdRP+E/1zKjmDJf94CJpd8yk4VGp1Pp5Tb4yIXsXRGNsOPt0LDOnVDwQngxVjzYbpMgsAYjKikxBmRHX5Mx85g== Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id vr4-20020a05620a55a400b00783f2040be5si2545351qkn.535.2024.02.09.10.33.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 09 Feb 2024 10:33:12 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=JMXp6bhd; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 567793858427 for ; Fri, 9 Feb 2024 18:33:12 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id E0724385841E for ; Fri, 9 Feb 2024 18:32:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E0724385841E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E0724385841E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707503547; cv=none; b=islRxVkuuX9SWKxU2+rUWJDSGGn/s/s66Ri88jFTGhRmj4mq07c222ay5ZGikp5CaA8x26xHHjI55+Wb7cDRoToIttAnaVPY01EqZ1W7KX1j8Eu0FpDnc/vZPPvQJcPp80NPujw1KedGTxaZJ46OOBO/xpjGDvLjq/LwXW9OL4U= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707503547; c=relaxed/simple; bh=Ai44rtrlSPXbKAHTrmndZIflPBotn8T7xVrPYdtOI0A=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=v3p5zGZHrW4QxmLTpNc7ZBY6g7xMrP6nVNsOqoG+qsowe4Yln4rc1Q5xItEWPk+p+0lSk42RVc/po2bs+IuUaocp5ASs1mpgFDISPhM2kKBajy+HL5s3+gRp7q3a8QigxFkdnnse6l7n9MuNwj23K3T7qlutPY/Slx4iouTeOPo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707503545; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=bpSHeHdhXafz1N33X/6tmA1+4rCWXuCbD7XSUUthjvQ=; b=JMXp6bhdHP6E6jxCoswCPLJOaLZnOuraUSgWyhuK8GXdKsBBBFrfZZqPODf6+SRfePBze/ hWD07yT61JhK8TT7YfFYyCoEH/CS90Dd9ernfyf7nawLWznWUsrvmnsOJi+jQptP//pS+V qL0PF2e7qmaly6lab6N+KMXnEjEp1R0= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-593-mZzbVwJgPaeXhmN_fioLvg-1; Fri, 09 Feb 2024 13:32:24 -0500 X-MC-Unique: mZzbVwJgPaeXhmN_fioLvg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 24EC910665A0 for ; Fri, 9 Feb 2024 18:32:24 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.8.227]) by smtp.corp.redhat.com (Postfix) with ESMTP id F052A1C10C0C; Fri, 9 Feb 2024 18:32:23 +0000 (UTC) From: Marek Polacek To: GCC Patches , Jason Merrill , Patrick Palka Subject: [PATCH] c++: fix ICE with __type_pack_element [PR113834] Date: Fri, 9 Feb 2024 13:32:16 -0500 Message-ID: <20240209183216.1287806-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790447287198268453 X-GMAIL-MSGID: 1790447287198268453 Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Here we crash on this invalid code because we seem to infinitely recurse and end up with __type_pack_element with index that doesn't tree_fits_shwi_p which then crashes on tree_to_shwi. Thanks to Jakub for suggesting a nicer fix than my original one. PR c++/113834 gcc/cp/ChangeLog: * semantics.cc (finish_type_pack_element): Perform range checking before tree_to_shwi. gcc/testsuite/ChangeLog: * g++.dg/ext/type_pack_element4.C: New test. --- gcc/cp/semantics.cc | 7 +++---- gcc/testsuite/g++.dg/ext/type_pack_element4.C | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/ext/type_pack_element4.C base-commit: c9bdcb0c3433ce09f5bb713a51a14130858578a2 diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 3299e270446..57840176863 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -4650,20 +4650,19 @@ finish_type_pack_element (tree idx, tree types, tsubst_flags_t complain) error ("%<__type_pack_element%> index is not an integral constant"); return error_mark_node; } - HOST_WIDE_INT val = tree_to_shwi (idx); - if (val < 0) + if (tree_int_cst_sgn (idx) < 0) { if (complain & tf_error) error ("%<__type_pack_element%> index is negative"); return error_mark_node; } - if (val >= TREE_VEC_LENGTH (types)) + if (wi::to_widest (idx) >= TREE_VEC_LENGTH (types)) { if (complain & tf_error) error ("%<__type_pack_element%> index is out of range"); return error_mark_node; } - return TREE_VEC_ELT (types, val); + return TREE_VEC_ELT (types, tree_to_shwi (idx)); } /* Implement the __direct_bases keyword: Return the direct base classes diff --git a/gcc/testsuite/g++.dg/ext/type_pack_element4.C b/gcc/testsuite/g++.dg/ext/type_pack_element4.C new file mode 100644 index 00000000000..aa508c79090 --- /dev/null +++ b/gcc/testsuite/g++.dg/ext/type_pack_element4.C @@ -0,0 +1,17 @@ +// PR c++/113834 +// { dg-do compile { target c++17 } } + +template class tuple{}; +template +__type_pack_element<__i, _Elements...> &get(tuple<_Elements...> &__t) noexcept; // { dg-error "index is out of range" } +tuple data; +template +unsigned take_impl(unsigned idx) { + if constexpr (Level != -1){ + return take_impl(get(data)); // { dg-error "" } + } + return 0; +} +int main() { + take_impl<2>(0); +}