From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 125155 invoked by alias); 16 Feb 2018 09:49:37 -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 125123 invoked by uid 89); 16 Feb 2018 09:49:36 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-12.2 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_LOW,T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy= X-HELO: mx1.redhat.com Received: from mx3-rdu2.redhat.com (HELO mx1.redhat.com) (66.187.233.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 16 Feb 2018 09:49:35 +0000 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2DBEAEAEA4 for ; Fri, 16 Feb 2018 09:49:30 +0000 (UTC) Received: from tucnak.zalov.cz (ovpn-204-85.brq.redhat.com [10.40.204.85]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E074B1007D90 for ; Fri, 16 Feb 2018 09:49:29 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.15.2/8.15.2) with ESMTP id w1G8SG8G031568; Fri, 16 Feb 2018 09:28:16 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.15.2/8.15.2/Submit) id w1G8SFpa031567; Fri, 16 Feb 2018 09:28:15 +0100 Date: Fri, 16 Feb 2018 09:49:00 -0000 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [C++ PATCH] Fix ICE with return in statement expression in constexpr.c (PR c++/84192) Message-ID: <20180216082814.GM5867@tucnak> Reply-To: Jakub Jelinek MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.9.1 (2017-09-22) X-IsSubscribed: yes X-SW-Source: 2018-02/txt/msg00963.txt.bz2 Hi! pop_stmt_list, if there is just a single stmt inside statement expression moves the stmt out of the STATEMENT_LIST (and I think cp_fold does too). We only initialize jump_target to non-NULL in cxx_eval_statement_list or for calls, so before we have a chance to diagnose the error of using an expression with void type, we ICE trying to dereference NULL jump_target. This can't happen with BREAK_STMT nor CONTINUE_STMT, because they are not potential constant expressions, and I think can only happen when ctx->quiet is true, otherwise it should have been diagnosed already before. If a RETURN_EXPR (or in theory break/continue) appears in a (potential) constant expression we want to evaluate, not doing anything with jump_target if we aren't inside a statement list makes sense to me, there is no following statement to bypass. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2018-02-16 Marek Polacek Jakub Jelinek PR c++/84192 * constexpr.c (cxx_eval_constant_expression) : Don't set *jump_target to anything if jump_target is NULL. * g++.dg/cpp1y/constexpr-84192.C: New test. --- gcc/cp/constexpr.c.jj 2018-02-12 19:17:37.937216029 +0100 +++ gcc/cp/constexpr.c 2018-02-15 16:10:56.630572360 +0100 @@ -4254,7 +4254,13 @@ cxx_eval_constant_expression (const cons r = cxx_eval_constant_expression (ctx, TREE_OPERAND (t, 0), lval, non_constant_p, overflow_p); - *jump_target = t; + if (jump_target) + *jump_target = t; + else + /* Can happen with ({ return true; }) && false; passed to + maybe_constant_value. There is nothing to jump over in this + case, and the bug will be diagnosed later. */ + gcc_assert (ctx->quiet); break; case SAVE_EXPR: --- gcc/testsuite/g++.dg/cpp1y/constexpr-84192.C.jj 2018-02-15 16:00:58.242588914 +0100 +++ gcc/testsuite/g++.dg/cpp1y/constexpr-84192.C 2018-02-15 16:01:30.219585291 +0100 @@ -0,0 +1,41 @@ +// PR c++/84192 +// { dg-do compile { target c++14 } } +// { dg-options "" } + +bool +f1 () +{ + return ({ return true; }) && false; // { dg-error "could not convert" } +} + +void +f2 () +{ + for (;;) + constexpr bool b = ({ break; false; }) && false; // { dg-error "statement is not a constant expression" } +} + +constexpr bool +f3 (int n) +{ + bool b = false; + for (int i = 0; i < n; i++) + b = ({ break; }); // { dg-error "void value not ignored as it ought to be" } + return b; +} + +constexpr bool b = f3 (4); + +bool +f4 () +{ + constexpr bool b = ({ return true; }) && false; // { dg-error "could not convert" } + return false; +} + +constexpr bool +f5 (int x) +{ + constexpr bool b = ({ switch (x) case 0: true; }) && false; // { dg-error "could not convert" } + return false; +} Jakub