Fix endless recursion due to type scoping
commitd7f2775af8d67d2c3b08823a69fae35d516dfea2
authorMichael Matz <matz@suse.de>
Sat, 13 Feb 2021 02:20:44 +0000 (13 03:20 +0100)
committerMichael Matz <matz@suse.de>
Sat, 13 Feb 2021 02:29:43 +0000 (13 03:29 +0100)
tree12e3b3f420e14142c9a9f5ed53efd2b025c403e7
parent24c94fff09ea21f2e70b575256824e4648124aad
Fix endless recursion due to type scoping

this change fixes building of invalid types.  The inner scope
struct P is return type of the forward decl foobar.  The outer scope
foobar() call implicitely declares that function again, with int
return type; overall this leads to access within the sym free list,
effectively building up a type directly referring to itself, leading
to endless recursion later.  The testcase is:

void n(void)
{
    {
      struct P {
          int __val;
      };
      struct P foobar(); // 1
    }
  foobar();  // 2
}

I've not included it in tests2 for now, because tcc accepts this.
Ideally we would like to reject it (as 'int foobar();' is incompatible
with the earlier decl).  clang also accepts it, but only because it's
not handling (1) above as an implicit decl of foobar (it warns, and
with -pedantic also warns about the type incompatiblity).  GCC rejects
this.

Implementing that in tcc requires some surgery, as we need to differ
between these cases:

  {  struct P foo(int); // 1
     foo();        // no implicit decl, call to foo from 1
  }

and

  { { struct P foo(int); // 2 }
    foo();         // implicit decl, _incompatible_ with 2
  }
tccgen.c