Always treat "struct T;" as declaring the tag within the current scope.

A declaration of this exact form always declares the tag T within the current scope, and as such makes this "struct T" a distinct type from any other "struct T" type in an outer scope. (Similarly for unions.)

See C17 section 6.7.2.3 p7 (and corresponding places in all other C standards).

Here is an example of a program affected by this:

struct S {char a;};
int main(void) {
        struct S;
        struct S *sp;
        struct S {long b;} s;
        sp = &s;
        sp->b = sizeof(*sp);
        return s.b;
}
This commit is contained in:
Stephen Heumann 2022-10-04 18:45:11 -05:00
parent 3cea478e5e
commit 995ded07a5
2 changed files with 7 additions and 1 deletions

View File

@ -3265,7 +3265,9 @@ while token.kind in allowedTokens do begin
NextToken; {skip the structure name}
if structPtr = nil then begin {if the name hasn't been defined then...}
if token.kind <> lbracech then
structPtr := FindSymbol(ttoken, tagSpace, false, true);
if (token.kind <> semicolonch) or
(myDeclarationModifiers <> []) then
structPtr := FindSymbol(ttoken, tagSpace, false, true);
if structPtr <> nil then
structTypePtr := structPtr^.itype
else begin

View File

@ -1899,6 +1899,10 @@ int foo(int[42]);
206. If an enum type defined in an outer scope was used in an inner scope, and then a new struct, union, or enum type was declared with the same tag in the same inner scope, a spurious error was reported.
(Bug fixes below here were added in ORCA/C 2.2.0 B7.)
207. If a struct or union type with a tag T has been declared within an outer scope, a declaration "struct T;" or "union T;" within an inner scope should declare a separate type, distinct from the one in the outer scope.
-- Bugs from C 2.1.0 that have been fixed -----------------------------------
1. In some situations, fread() reread the first 1K or so of the file.