Detect invalid VLA decls

see testcase, when the inner array dimension of multi-dimensional
VLAs isn't given TCC was generating invalid vstack accesses.
Those are actually invalid, so just diagnose them.
This commit is contained in:
Michael Matz 2019-04-07 04:09:25 +02:00
parent 5ac2a26666
commit 2a417b50ee
3 changed files with 12 additions and 0 deletions

View File

@ -4521,6 +4521,7 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td)
} else { } else {
if (!is_integer_btype(vtop->type.t & VT_BTYPE)) if (!is_integer_btype(vtop->type.t & VT_BTYPE))
tcc_error("size of variable length array should be an integer"); tcc_error("size of variable length array should be an integer");
n = 0;
t1 = VT_VLA; t1 = VT_VLA;
} }
} }
@ -4532,6 +4533,8 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td)
t1 |= type->t & VT_VLA; t1 |= type->t & VT_VLA;
if (t1 & VT_VLA) { if (t1 & VT_VLA) {
if (n < 0)
tcc_error("need explicit inner array size in VLAs");
loc -= type_size(&int_type, &align); loc -= type_size(&int_type, &align);
loc &= -align; loc &= -align;
n = loc; n = loc;

View File

@ -166,4 +166,10 @@ union u {
int i; int i;
} m; } m;
}; };
#elif defined test_vla_1
int X=1;
int main(void) {
int t[][][X];
}
#endif #endif

View File

@ -77,3 +77,6 @@
[test_nested_types] [test_nested_types]
60_errors_and_warnings.c:166: error: struct/union/enum already defined 60_errors_and_warnings.c:166: error: struct/union/enum already defined
[test_vla_1]
60_errors_and_warnings.c:173: error: need explicit inner array size in VLAs