From cbbba01b463066593ce1f524f4fbda49331b7488 Mon Sep 17 00:00:00 2001 From: Pascal Cuoq Date: Mon, 24 Jun 2019 10:28:36 +0200 Subject: [PATCH] reject invalid arrays in post_type() --- tccgen.c | 11 ++++++++--- tests/tests2/60_errors_and_warnings.c | 16 ++++++++++++++++ tests/tests2/60_errors_and_warnings.expect | 15 +++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/tccgen.c b/tccgen.c index 3726109f..a3096e3d 100644 --- a/tccgen.c +++ b/tccgen.c @@ -4377,7 +4377,7 @@ static int asm_label_instr(void) static int post_type(CType *type, AttributeDef *ad, int storage, int td) { - int n, l, t1, arg_size, align; + int n, l, t1, arg_size, align, unused_align; Sym **plast, *s, *first; AttributeDef ad1; CType pt; @@ -4503,10 +4503,15 @@ static int post_type(CType *type, AttributeDef *ad, int storage, int td) skip(']'); /* parse next post type */ post_type(type, ad, storage, 0); - if (type->t == VT_FUNC) + + if ((type->t & VT_BTYPE) == VT_FUNC) tcc_error("declaration of an array of functions"); + if ((type->t & VT_BTYPE) == VT_VOID + || type_size(type, &unused_align) < 0) + tcc_error("declaration of an array of incomplete type elements"); + t1 |= type->t & VT_VLA; - + if (t1 & VT_VLA) { if (n < 0) tcc_error("need explicit inner array size in VLAs"); diff --git a/tests/tests2/60_errors_and_warnings.c b/tests/tests2/60_errors_and_warnings.c index cf8c1f18..2101c831 100644 --- a/tests/tests2/60_errors_and_warnings.c +++ b/tests/tests2/60_errors_and_warnings.c @@ -182,4 +182,20 @@ void * _Alignas(16) p1; _Static_assert(ONE == 0, "don't show me this"); _Static_assert(ONE == 1, "ONE is not 1"); +#elif defined test_void_array + void t[3]; + +#elif defined test_incomplete_enum_array + enum e t[3]; + +#elif defined test_incomplete_struct_array + struct s t[3]; + +#elif defined test_const_fun_array + typedef void f(void); + const f t[3]; + +#elif defined test_incomplete_array_array + int t[][3]; + #endif diff --git a/tests/tests2/60_errors_and_warnings.expect b/tests/tests2/60_errors_and_warnings.expect index 7d55eeeb..53d62cfa 100644 --- a/tests/tests2/60_errors_and_warnings.expect +++ b/tests/tests2/60_errors_and_warnings.expect @@ -86,3 +86,18 @@ [test_static_assert] 60_errors_and_warnings.c:183: error: "ONE is not 1" + +[test_void_array] +60_errors_and_warnings.c:186: error: declaration of an array of incomplete type elements + +[test_incomplete_enum_array] +60_errors_and_warnings.c:189: error: declaration of an array of incomplete type elements + +[test_incomplete_struct_array] +60_errors_and_warnings.c:192: error: declaration of an array of incomplete type elements + +[test_const_fun_array] +60_errors_and_warnings.c:196: error: declaration of an array of functions + +[test_incomplete_array_array] +60_errors_and_warnings.c:199: error: unknown type size