From 2a417b50ee350589efd3018fb75ec8a6f1993d7a Mon Sep 17 00:00:00 2001 From: Michael Matz Date: Sun, 7 Apr 2019 04:09:25 +0200 Subject: [PATCH] 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. --- tccgen.c | 3 +++ tests/tests2/60_errors_and_warnings.c | 6 ++++++ tests/tests2/60_errors_and_warnings.expect | 3 +++ 3 files changed, 12 insertions(+) diff --git a/tccgen.c b/tccgen.c index 39f9a28d..cab801ae 100644 --- a/tccgen.c +++ b/tccgen.c @@ -4521,6 +4521,7 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td) } else { if (!is_integer_btype(vtop->type.t & VT_BTYPE)) tcc_error("size of variable length array should be an integer"); + n = 0; t1 = VT_VLA; } } @@ -4532,6 +4533,8 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td) t1 |= type->t & 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 &= -align; n = loc; diff --git a/tests/tests2/60_errors_and_warnings.c b/tests/tests2/60_errors_and_warnings.c index 44f7f67e..61561b8e 100644 --- a/tests/tests2/60_errors_and_warnings.c +++ b/tests/tests2/60_errors_and_warnings.c @@ -166,4 +166,10 @@ union u { int i; } m; }; +#elif defined test_vla_1 +int X=1; + +int main(void) { + int t[][][X]; +} #endif diff --git a/tests/tests2/60_errors_and_warnings.expect b/tests/tests2/60_errors_and_warnings.expect index 3b34fef9..e4ddd9de 100644 --- a/tests/tests2/60_errors_and_warnings.expect +++ b/tests/tests2/60_errors_and_warnings.expect @@ -77,3 +77,6 @@ [test_nested_types] 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