]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
vala: Set error and return early on invalid index in ElementAccess
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 18 Feb 2020 15:35:21 +0000 (16:35 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Tue, 18 Feb 2020 16:19:39 +0000 (17:19 +0100)
This caused criticals like:

  vala_code_node_check: assertion 'self != NULL' failed

tests/Makefile.am
tests/semantic/element-access-index-invalid.test [new file with mode: 0644]
vala/valaelementaccess.vala

index 98c5e5faff14dbbff40f0ff29bd5a9fcb453a51c..9586b63ec66e57983a8d97483969d7cb9eced4a3 100644 (file)
@@ -744,6 +744,7 @@ TESTS = \
        semantic/delegate-return-valist.test \
        semantic/delegate-too-few-type-arguments.test \
        semantic/delegate-too-many-type-arguments.test \
+       semantic/element-access-index-invalid.test \
        semantic/enum-empty.test \
        semantic/errordomain-empty.test \
        semantic/field-accessibility.test \
diff --git a/tests/semantic/element-access-index-invalid.test b/tests/semantic/element-access-index-invalid.test
new file mode 100644 (file)
index 0000000..f653e8c
--- /dev/null
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main () {
+       int foo = 23;
+       var bar = foo["G"];
+}
index 9ecc6c67e8d96ae2704c85345f6bcbe58961e84d..40a8c196bb7abb79d36a172d5eecc3131f656b22 100644 (file)
@@ -195,8 +195,10 @@ public class Vala.ElementAccess : Expression {
                        }
 
                        if (array_type.rank < get_indices ().size) {
+                               error = true;
                                Report.error (source_reference, "%d extra indices for element access".printf (get_indices ().size - array_type.rank));
                        } else if (array_type.rank > get_indices ().size) {
+                               error = true;
                                Report.error (source_reference, "%d missing indices for element access".printf (array_type.rank - get_indices ().size));
                        }
                } else if (pointer_type != null && !pointer_type.base_type.is_reference_type_or_type_parameter ()) {
@@ -229,6 +231,7 @@ public class Vala.ElementAccess : Expression {
 
                        error = true;
                        Report.error (source_reference, "The expression `%s' does not denote an array".printf (container.value_type.to_string ()));
+                       return false;
                }
 
                if (index_int_type_check) {