]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PR c++/90825 - endless recursion when evaluating sizeof.
authorMarek Polacek <polacek@redhat.com>
Thu, 13 Jun 2019 00:56:54 +0000 (00:56 +0000)
committerMarek Polacek <mpolacek@gcc.gnu.org>
Thu, 13 Jun 2019 00:56:54 +0000 (00:56 +0000)
PR c++/90832 - endless recursion when evaluating sizeof.
* constexpr.c (cxx_eval_constant_expression): Don't recurse on the
result of fold_sizeof_expr if is returns a SIZEOF_EXPR.
* typeck.c (cxx_sizeof_expr): Only return a SIZEOF_EXPR if the operand
is instantiation-dependent.

* g++.dg/cpp0x/constexpr-sizeof2.C: New test.
* g++.dg/cpp0x/constexpr-sizeof3.C: New test.

From-SVN: r272229

gcc/cp/ChangeLog
gcc/cp/constexpr.c
gcc/cp/typeck.c
gcc/testsuite/g++.dg/cpp0x/constexpr-sizeof2.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp0x/constexpr-sizeof3.C [new file with mode: 0644]

index af8a3ccbe1d3d40fd555938c60c69a47ace08ef1..881dcfb7af942597e469770100cad7cb2ef89945 100644 (file)
@@ -1,5 +1,12 @@
 2019-06-12  Marek Polacek  <polacek@redhat.com>
 
+       PR c++/90825 - endless recursion when evaluating sizeof.
+       PR c++/90832 - endless recursion when evaluating sizeof.
+       * constexpr.c (cxx_eval_constant_expression): Don't recurse on the
+       result of fold_sizeof_expr if is returns a SIZEOF_EXPR.
+       * typeck.c (cxx_sizeof_expr): Only return a SIZEOF_EXPR if the operand
+       is instantiation-dependent.
+
        PR c++/90736 - bogus error with alignof.
        * constexpr.c (adjust_temp_type): Use cv_unqualified type.
 
index 96558b839ab29a559296ceedb14f7c6b210dbff2..f6f21b33396c9886ebf9ccf092114d1aa29356bc 100644 (file)
@@ -4765,9 +4765,19 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
       break;
 
     case SIZEOF_EXPR:
-      r = cxx_eval_constant_expression (ctx, fold_sizeof_expr (t), lval,
-                                       non_constant_p, overflow_p,
-                                       jump_target);
+      r = fold_sizeof_expr (t);
+      /* In a template, fold_sizeof_expr may merely create a new SIZEOF_EXPR,
+        which could lead to an infinite recursion.  */
+      if (TREE_CODE (r) != SIZEOF_EXPR)
+       r = cxx_eval_constant_expression (ctx, r, lval,
+                                         non_constant_p, overflow_p,
+                                         jump_target);
+      else
+       {
+         *non_constant_p = true;
+         gcc_assert (ctx->quiet);
+       }
+
       break;
 
     case COMPOUND_EXPR:
index c107a321949bd6eeab484ac7836a89a9323fd7a9..71daf32733a302a674f03a0f6647e8033cdbbb0c 100644 (file)
@@ -1691,7 +1691,7 @@ cxx_sizeof_expr (tree e, tsubst_flags_t complain)
   if (e == error_mark_node)
     return error_mark_node;
 
-  if (processing_template_decl)
+  if (instantiation_dependent_uneval_expression_p (e))
     {
       e = build_min (SIZEOF_EXPR, size_type_node, e);
       TREE_SIDE_EFFECTS (e) = 0;
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-sizeof2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-sizeof2.C
new file mode 100644 (file)
index 0000000..8676ae4
--- /dev/null
@@ -0,0 +1,14 @@
+// PR c++/90825 - endless recursion when evaluating sizeof.
+// { dg-do compile { target c++11 } }
+
+class address {
+  char host_[63];
+public:
+  static constexpr unsigned buffer_size() noexcept { return sizeof(host_); }
+};
+
+template <class Archive>
+void load()
+{
+  char host[address::buffer_size()];
+}
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-sizeof3.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-sizeof3.C
new file mode 100644 (file)
index 0000000..05f07c3
--- /dev/null
@@ -0,0 +1,22 @@
+// PR c++/90832 - endless recursion when evaluating sizeof.
+// { dg-do compile { target c++11 } }
+
+class B
+{
+ template <typename T> friend struct A;
+ B() {}
+};
+
+template <typename T>
+struct A
+{
+ A() noexcept(sizeof(B{})) { }
+};
+
+struct C
+{
+ C()
+ {
+ static_assert( sizeof(A<int>{}), "" );
+ }
+};