Skip to content

Commit

Permalink
Merge pull request cc65#2199 from acqn/FAMFix
Browse files Browse the repository at this point in the history
[cc65] Forbidden struct itself with flexible array member as struct member or array element
  • Loading branch information
mrdudz authored Oct 5, 2023
2 parents 327281b + fc60312 commit 1219379
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/cc65/declare.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,13 @@ static void CheckArrayElementType (Type* DataType)
if (!IsTypeVoid (T) || IS_Get (&Standard) != STD_CC65) {
Error ("Array of 0-size element type '%s'", GetFullTypeName (T));
}
} else {
if (IsTypeStruct (T)) {
SymEntry* TagEntry = GetESUTagSym (T);
if (TagEntry && SymHasFlexibleArrayMember (TagEntry)) {
Error ("Invalid use of struct with flexible array member");
}
}
}
} else {
++T;
Expand Down Expand Up @@ -1193,6 +1200,9 @@ static SymEntry* ParseStructSpec (const char* Name, unsigned* DSFlags)
if (TagEntry && SymHasFlexibleArrayMember (TagEntry)) {
Field->Flags |= SC_HAVEFAM;
Flags |= SC_HAVEFAM;
if (IsTypeStruct (Decl.Type)) {
Error ("Invalid use of struct with flexible array member");
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions test/err/bug2016-fam-member.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* Bug #2016 - cc65 erroneously allows struct fields that are structs with flexible array members */

typedef struct x {
int a;
int b[]; /* Ok: Flexible array member can be last */
} x;

struct y {
x x; /* Not ok: Contains flexible array member */
int a;
};
9 changes: 9 additions & 0 deletions test/err/bug2017-fam-element.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* Bug #2017 - cc65 erroneously allows arrays of structs with flexible array members */

struct z {
int a;
int c;
int b[];
};

struct z y[3]; /* Should be an error */

0 comments on commit 1219379

Please sign in to comment.