https://gcc.gnu.org/g:0c27eade4754c13a54e265e4305182c95be1e441

commit r15-1952-g0c27eade4754c13a54e265e4305182c95be1e441
Author: Marek Polacek <pola...@redhat.com>
Date:   Tue Jun 25 14:55:08 2024 -0400

    c: ICE with invalid sizeof [PR115642]
    
    Here we ICE in c_expr_sizeof_expr on an erroneous expr.value.  The
    code checks for expr.value == error_mark_node but here the e_m_n is
    wrapped in a C_MAYBE_CONST_EXPR.  I don't think we should have created
    such a tree, so let's return earlier in c_cast_expr.
    
            PR c/115642
    
    gcc/c/ChangeLog:
    
            * c-typeck.cc (c_cast_expr): Return error_mark_node if build_c_cast
            failed.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/noncompile/sizeof-1.c: New test.

Diff:
---
 gcc/c/c-typeck.cc                          | 3 +++
 gcc/testsuite/gcc.dg/noncompile/sizeof-1.c | 7 +++++++
 2 files changed, 10 insertions(+)

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index 455dc374b481..36f88fcd03de 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -6702,6 +6702,9 @@ c_cast_expr (location_t loc, struct c_type_name 
*type_name, tree expr)
     return error_mark_node;
 
   ret = build_c_cast (loc, type, expr);
+  if (ret == error_mark_node)
+    return error_mark_node;
+
   if (type_expr)
     {
       bool inner_expr_const = true;
diff --git a/gcc/testsuite/gcc.dg/noncompile/sizeof-1.c 
b/gcc/testsuite/gcc.dg/noncompile/sizeof-1.c
new file mode 100644
index 000000000000..db7e2044b113
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/noncompile/sizeof-1.c
@@ -0,0 +1,7 @@
+/* PR c/115642 */
+/* { dg-do compile } */
+
+void f (int N) {
+  int a[2][N];
+  sizeof ((int [2][N])a); /* { dg-error "cast specifies array type" } */
+}

Reply via email to