From fc603129da73bd5b3daed358479b99f140b521f0 Mon Sep 17 00:00:00 2001 From: acqn Date: Mon, 18 Sep 2023 15:44:58 +0800 Subject: [PATCH] A structure with a flexible array member shall not be a member of a structure or an element of an array according to the ISO C Standard. --- src/cc65/declare.c | 10 ++++++++++ test/err/bug2016-fam-member.c | 11 +++++++++++ test/err/bug2017-fam-element.c | 9 +++++++++ 3 files changed, 30 insertions(+) create mode 100644 test/err/bug2016-fam-member.c create mode 100644 test/err/bug2017-fam-element.c diff --git a/src/cc65/declare.c b/src/cc65/declare.c index 59eb555c4..80be9ceb7 100644 --- a/src/cc65/declare.c +++ b/src/cc65/declare.c @@ -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; @@ -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"); + } } } diff --git a/test/err/bug2016-fam-member.c b/test/err/bug2016-fam-member.c new file mode 100644 index 000000000..02c9ec275 --- /dev/null +++ b/test/err/bug2016-fam-member.c @@ -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; +}; diff --git a/test/err/bug2017-fam-element.c b/test/err/bug2017-fam-element.c new file mode 100644 index 000000000..195ca6597 --- /dev/null +++ b/test/err/bug2017-fam-element.c @@ -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 */