C PATCH to fix ICE with -Wsizeof-array-argument (PR c/82167)

Message ID 20170913110032.GL20631@redhat.com
State New
Headers show
Series
  • C PATCH to fix ICE with -Wsizeof-array-argument (PR c/82167)
Related show

Commit Message

Marek Polacek Sept. 13, 2017, 11 a.m.
This fixes a segv, where -Wsizeof-array-argument crashed because
expr.original_type was null.  It was null because for sizeof (*&a)
we go to c_parser_unary_expression's case CPP_AND: and case CPP_MULT:,
and that removes the original_type.  But we don't need the original 
type, we can just use TREE_TYPE of expr.value.

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

2017-09-13  Marek Polacek  <polacek@redhat.com>

	PR c/82167
	* c-typeck.c (c_expr_sizeof_expr): Use the type of expr.value rather
	than expr.original_type.

	* gcc.dg/pr82167.c: New test.


	Marek

Comments

Jeff Law Sept. 13, 2017, 3:07 p.m. | #1
On 09/13/2017 05:00 AM, Marek Polacek wrote:
> This fixes a segv, where -Wsizeof-array-argument crashed because
> expr.original_type was null.  It was null because for sizeof (*&a)
> we go to c_parser_unary_expression's case CPP_AND: and case CPP_MULT:,
> and that removes the original_type.  But we don't need the original 
> type, we can just use TREE_TYPE of expr.value.
> 
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
> 
> 2017-09-13  Marek Polacek  <polacek@redhat.com>
> 
> 	PR c/82167
> 	* c-typeck.c (c_expr_sizeof_expr): Use the type of expr.value rather
> 	than expr.original_type.
> 
> 	* gcc.dg/pr82167.c: New test.
OK.
jeff

Patch

diff --git gcc/c/c-typeck.c gcc/c/c-typeck.c
index 91996c95ed0..f45fd3cfbbf 100644
--- gcc/c/c-typeck.c
+++ gcc/c/c-typeck.c
@@ -2909,7 +2909,7 @@  c_expr_sizeof_expr (location_t loc, struct c_expr expr)
 	  if (warning_at (loc, OPT_Wsizeof_array_argument,
 			  "%<sizeof%> on array function parameter %qE will "
 			  "return size of %qT", expr.value,
-			  expr.original_type))
+			  TREE_TYPE (expr.value)))
 	    inform (DECL_SOURCE_LOCATION (expr.value), "declared here");
 	}
       tree folded_expr = c_fully_fold (expr.value, require_constant_value,
diff --git gcc/testsuite/gcc.dg/pr82167.c gcc/testsuite/gcc.dg/pr82167.c
index e69de29bb2d..af3b3a5a1c9 100644
--- gcc/testsuite/gcc.dg/pr82167.c
+++ gcc/testsuite/gcc.dg/pr82167.c
@@ -0,0 +1,14 @@ 
+/* PR c/82167 */
+/* { dg-do compile } */
+
+void
+fn1 (int a[])
+{
+  __builtin_printf ("%zu\n", sizeof (*&a)); /* { dg-warning ".sizeof. on array function parameter .a. will return size of .int \\*." } */
+}
+
+void
+fn2 (int *a[])
+{
+  __builtin_printf ("%zu\n", sizeof (*&a)); /* { dg-warning ".sizeof. on array function parameter .a. will return size of .int \\*\\*." } */
+}