summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRob Pilling <robpilling@gmail.com>2023-11-26 09:29:06 +0000
committerRob Pilling <robpilling@gmail.com>2023-11-26 09:29:06 +0000
commitfb164e0ab48b64fe32ffd56a896ccff6ab6c9549 (patch)
treedef31ca7143b2d386293f3c3522dd626c770476f
parentbe8f8947106854cb0eca11691aa4dc99fb4cf845 (diff)
Error out for incomplete type initialisation
-rw-r--r--tccgen.c5
-rw-r--r--tests/tests2/60_errors_and_warnings.c13
-rw-r--r--tests/tests2/60_errors_and_warnings.expect5
3 files changed, 22 insertions, 1 deletions
diff --git a/tccgen.c b/tccgen.c
index e640e78..29dae91 100644
--- a/tccgen.c
+++ b/tccgen.c
@@ -7996,6 +7996,11 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r,
toplevel array or the last member of the toplevel struct */
if (size < 0) {
+ // error out except for top-level incomplete arrays
+ // (arrays of incomplete types are handled in array parsing)
+ if (!(type->t & VT_ARRAY))
+ tcc_error("initialization of incomplete type");
+
/* If the base type itself was an array type of unspecified size
(like in 'typedef int arr[]; arr x = {1};') then we will
overwrite the unknown size by the real one for this decl.
diff --git a/tests/tests2/60_errors_and_warnings.c b/tests/tests2/60_errors_and_warnings.c
index 960c64f..6513ee1 100644
--- a/tests/tests2/60_errors_and_warnings.c
+++ b/tests/tests2/60_errors_and_warnings.c
@@ -463,4 +463,17 @@ int main() {
#error \123\\
456
+#elif defined test_error_incomplete_type
+struct A;
+void f(struct A *);
+
+int main()
+{
+ f(&(struct A){});
+}
+
+struct A {
+ int x;
+};
+
#endif
diff --git a/tests/tests2/60_errors_and_warnings.expect b/tests/tests2/60_errors_and_warnings.expect
index 9a95e93..25a7624 100644
--- a/tests/tests2/60_errors_and_warnings.expect
+++ b/tests/tests2/60_errors_and_warnings.expect
@@ -23,7 +23,7 @@
[returns 1]
[test_61_undefined_enum]
-60_errors_and_warnings.c:46: error: unknown type size
+60_errors_and_warnings.c:46: error: initialization of incomplete type
[test_74_non_const_init]
60_errors_and_warnings.c:49: error: initializer element is not constant
@@ -228,3 +228,6 @@ arg[1] = "Y"
[test_error_string]
60_errors_and_warnings.c:464: error: #error \123\456
+
+[test_error_incomplete_type]
+60_errors_and_warnings.c:472: error: initialization of incomplete type