public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c: ICE with invalid sizeof [PR115642]
@ 2024-06-26 15:09 Marek Polacek
  2024-06-27 21:08 ` Marek Polacek
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2024-06-26 15:09 UTC (permalink / raw)
  To: GCC Patches, Joseph Myers

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
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.
---
 gcc/c/c-typeck.cc                          | 3 +++
 gcc/testsuite/gcc.dg/noncompile/sizeof-1.c | 7 +++++++
 2 files changed, 10 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/noncompile/sizeof-1.c

diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index ffcab7df4d3..8c03a7731c4 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -6695,6 +6695,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 00000000000..db7e2044b11
--- /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" } */
+}

base-commit: 47b68cda2c4afe32e84c5f18da0196c39e5e0edf
-- 
2.45.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c: ICE with invalid sizeof [PR115642]
  2024-06-26 15:09 [PATCH] c: ICE with invalid sizeof [PR115642] Marek Polacek
@ 2024-06-27 21:08 ` Marek Polacek
  0 siblings, 0 replies; 2+ messages in thread
From: Marek Polacek @ 2024-06-27 21:08 UTC (permalink / raw)
  To: GCC Patches, Joseph Myers

Sorry, I used the wrong e-mail address for Joseph.

On Wed, Jun 26, 2024 at 11:09:37AM -0400, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> 
> -- >8 --
> 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.
> ---
>  gcc/c/c-typeck.cc                          | 3 +++
>  gcc/testsuite/gcc.dg/noncompile/sizeof-1.c | 7 +++++++
>  2 files changed, 10 insertions(+)
>  create mode 100644 gcc/testsuite/gcc.dg/noncompile/sizeof-1.c
> 
> diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
> index ffcab7df4d3..8c03a7731c4 100644
> --- a/gcc/c/c-typeck.cc
> +++ b/gcc/c/c-typeck.cc
> @@ -6695,6 +6695,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 00000000000..db7e2044b11
> --- /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" } */
> +}
> 
> base-commit: 47b68cda2c4afe32e84c5f18da0196c39e5e0edf
> -- 
> 2.45.2
> 

Marek


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-06-27 21:08 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-26 15:09 [PATCH] c: ICE with invalid sizeof [PR115642] Marek Polacek
2024-06-27 21:08 ` Marek Polacek

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).