[vala/0.46] vala: Set error and return early on invalid index in ElementAccess



commit eecaa1757ef543c25276aa692f508417f18d164d
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Feb 18 16:35:21 2020 +0100

    vala: Set error and return early on invalid index in ElementAccess
    
    This caused criticals like:
    
      vala_code_node_check: assertion 'self != NULL' failed

 tests/Makefile.am                                | 1 +
 tests/semantic/element-access-index-invalid.test | 6 ++++++
 vala/valaelementaccess.vala                      | 3 +++
 3 files changed, 10 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 16812f4c0..9c18a40d9 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -721,6 +721,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
index 000000000..f653e8c92
--- /dev/null
+++ b/tests/semantic/element-access-index-invalid.test
@@ -0,0 +1,6 @@
+Invalid Code
+
+void main () {
+       int foo = 23;
+       var bar = foo["G"];
+}
diff --git a/vala/valaelementaccess.vala b/vala/valaelementaccess.vala
index b847e9515..08844e0f7 100644
--- a/vala/valaelementaccess.vala
+++ b/vala/valaelementaccess.vala
@@ -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) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]