public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "ppalka at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/99700] [10/11 Regression] uninitialized variable accepted as a constant expression in C++ 20 Date: Thu, 15 Apr 2021 16:55:15 +0000 [thread overview] Message-ID: <bug-99700-4-qxheaquenl@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-99700-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99700 Patrick Palka <ppalka at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |ppalka at gcc dot gnu.org Assignee|unassigned at gcc dot gnu.org |ppalka at gcc dot gnu.org Status|NEW |ASSIGNED --- Comment #3 from Patrick Palka <ppalka at gcc dot gnu.org> --- It seems reduced_constant_expression_p needs to check that a CONSTRUCTOR_NO_CLEARING array ctor initializes all elements. I'm testing: diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index c8d9dae36fb..6e530f9c88d 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -46,6 +46,7 @@ do { \ static HOST_WIDE_INT find_array_ctor_elt (tree ary, tree dindex, bool insert = false); +static int array_index_cmp (tree key, tree index); /* Returns true iff FUN is an instantiation of a constexpr function template or a defaulted constexpr function. */ @@ -2912,7 +2913,25 @@ reduced_constant_expression_p (tree t) else if (cxx_dialect >= cxx20 /* An ARRAY_TYPE doesn't have any TYPE_FIELDS. */ && TREE_CODE (TREE_TYPE (t)) == ARRAY_TYPE) - field = NULL_TREE; + { + tree min = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); + tree max = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (t))); + if (find_array_ctor_elt (t, min) == -1 + || find_array_ctor_elt (t, max) == -1) + return false; + tree cursor = size_zero_node; + FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (t), i, idx, val) + { + if (!reduced_constant_expression_p (val)) + return false; + if (array_index_cmp (cursor, idx) != 0) + return false; + if (TREE_CODE (idx) == RANGE_EXPR) + cursor = TREE_OPERAND (idx, 1); + cursor = int_const_binop (PLUS_EXPR, cursor, size_one_node); + } + return true; + } else if (cxx_dialect >= cxx20 && TREE_CODE (TREE_TYPE (t)) == UNION_TYPE) {
next prev parent reply other threads:[~2021-04-15 16:55 UTC|newest] Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-03-21 18:22 [Bug c++/99700] New: gcc takes an uninitialized variable as a constant expression hewillk at gmail dot com 2021-04-15 0:14 ` [Bug c++/99700] uninitialized variable accepted as a constant expression in C++ 20 msebor at gcc dot gnu.org 2021-04-15 0:15 ` [Bug c++/99700] [10/11 Regression] " msebor at gcc dot gnu.org 2021-04-15 16:55 ` ppalka at gcc dot gnu.org [this message] 2021-04-16 13:25 ` cvs-commit at gcc dot gnu.org 2021-04-16 13:27 ` [Bug c++/99700] [10 " ppalka at gcc dot gnu.org 2022-05-11 17:07 ` ppalka at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-99700-4-qxheaquenl@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).