Patchwork [c] : Fix PR52290, [4.4/4.5/4.6/4.7 Regression] internal compiler error: tree check: expected function_decl, have var_decl in start_function, at c-decl.c:7712

login
register
mail settings
Submitter Uros Bizjak
Date Feb. 23, 2012, 8:56 a.m.
Message ID <CAFULd4aj4YBEO+=FyNU8hoxERjcQTE3UPH-jMTv0iQ0yy3R41Q@mail.gmail.com>
Download mbox | patch
Permalink /patch/142572/
State New
Headers show

Comments

Uros Bizjak - Feb. 23, 2012, 8:56 a.m.
Hello!

With invalid code, we can trick grokdeclarator to return VAR_DECL,
even when FUNCDEF context is requested. Attached one-liner detects
this situation and exits early from start_function. The new error
stream looks correct to me, with following invalid testcase we get:

$ cat pr52290.c
int f()[j]

$ ~/gcc-build-fast/gcc/cc1 pr52290.c
pr52290.c:3:9: error: ‘j’ undeclared here (not in a function)
pr52290.c:3:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’
at end of input

2012-02-23  Uros Bizjak  <ubizjak@gmail.com>

	* c-decl.c (start_function): Exit early if decl1 is not FUNTION_DECL.

testsuite/ChangeLog:

2012-02-23  Uros Bizjak  <ubizjak@gmail.com>

	* gcc.dg/noncompile/pr52290.c: New test.

Patch was bootstrapped and regression tested on x86_64-pc-linux-gnu.

OK for mainline and release branches?

Uros.
Joseph S. Myers - Feb. 23, 2012, 4:27 p.m.
On Thu, 23 Feb 2012, Uros Bizjak wrote:

> 2012-02-23  Uros Bizjak  <ubizjak@gmail.com>
> 
> 	* c-decl.c (start_function): Exit early if decl1 is not FUNTION_DECL.
> 
> testsuite/ChangeLog:
> 
> 2012-02-23  Uros Bizjak  <ubizjak@gmail.com>
> 
> 	* gcc.dg/noncompile/pr52290.c: New test.
> 
> Patch was bootstrapped and regression tested on x86_64-pc-linux-gnu.
> 
> OK for mainline and release branches?

OK.

Patch

Index: c-decl.c
===================================================================
--- c-decl.c	(revision 184501)
+++ c-decl.c	(working copy)
@@ -7702,7 +7702,8 @@  start_function (struct c_declspecs *declspecs, str
 
   /* If the declarator is not suitable for a function definition,
      cause a syntax error.  */
-  if (decl1 == 0)
+  if (decl1 == 0
+      || TREE_CODE (decl1) != FUNCTION_DECL)
     return 0;
 
   loc = DECL_SOURCE_LOCATION (decl1);
Index: testsuite/gcc.dg/noncompile/pr52290.c
===================================================================
--- testsuite/gcc.dg/noncompile/pr52290.c	(revision 0)
+++ testsuite/gcc.dg/noncompile/pr52290.c	(revision 0)
@@ -0,0 +1,3 @@ 
+/* { dg-error "undeclared here" "" { target *-*-* } 3 } */
+/* { dg-error "expected" "" { target *-*-* } 3 } */
+int f()[j]