History

Detect usage of incomplete types inside struct/union

Make sure the only exception is for a flexible array member as the last element of a structure

Amine Najahi committed 13 years ago

Commit 3b07a151 parent d0c2f001 file · +12 −1Browse files

Unified · Split

tccgen.c+12−1
@@ -2740,7 +2740,7 @@ static void parse_attribute(AttributeDef *ad) · 2739 unmodified lines
27402740 /* enum/struct/union declaration. u is either VT_ENUM or VT_STRUCT */
27412741 static void struct_decl(CType *type, int u, int tdef)
27422742 {
2743− int a, v, size, align, maxalign, c, offset;
2743+ int a, v, size, align, maxalign, c, offset, flexible;
27442744 int bit_size, bit_pos, bsize, bt, lbit_pos, prevbt;
27452745 Sym *s, *ss, *ass, **ps;
27462746 AttributeDef ad;
@@ -2814,9 +2814,13 @@ static void struct_decl(CType *type, int u, int tdef) · 67 unmodified lines
28142814 prevbt = VT_INT;
28152815 bit_pos = 0;
28162816 offset = 0;
2817+ flexible = 0;
28172818 while (tok != '}') {
28182819 parse_btype(&btype, &ad);
28192820 while (1) {
2821+ if (flexible)
2822+ tcc_error("flexible array member '%s' not at the end of struct",
2823+ get_tok_str(v, NULL));
28202824 bit_size = -1;
28212825 v = 0;
28222826 type1 = btype;
@@ -2824,6 +2828,13 @@ static void struct_decl(CType *type, int u, int tdef) · 1 unmodified line
28242828 type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT);
28252829 if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT)
28262830 expect("identifier");
2831+ if (type_size(&type1, &align) < 0) {
2832+ if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY) && c)
2833+ flexible = 1;
2834+ else
2835+ tcc_error("field '%s' has incomplete type",
2836+ get_tok_str(v, NULL));
2837+ }
28272838 if ((type1.t & VT_BTYPE) == VT_FUNC ||
28282839 (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE)))
28292840 tcc_error("invalid type for '%s'",

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t