]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Reset outdated array _size_ variable after use as ref parameter 5490b0b21a4089d803d51c91311430d60d266f7b
authorRico Tzschichholz <ricotz@ubuntu.com>
Thu, 12 Mar 2020 18:19:19 +0000 (19:19 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Thu, 12 Mar 2020 18:19:19 +0000 (19:19 +0100)
Fixes https://gitlab.gnome.org/GNOME/vala/issues/929

codegen/valaccodemethodcallmodule.vala
tests/Makefile.am
tests/methods/parameter-ref-array-resize.vala [new file with mode: 0644]

index e43ff8be78b1df23089d50741d406cf589f3fb30..17df1cda43933ffe93e4c09c752310ffdd7cfcd4 100644 (file)
@@ -896,6 +896,20 @@ public class Vala.CCodeMethodCallModule : CCodeAssignmentModule {
                        }
 
                        var unary = arg as UnaryExpression;
+
+                       // update possible stale _*_size_ variable
+                       if (unary != null && unary.operator == UnaryOperator.REF) {
+                               if (get_ccode_array_length (param) && param.variable_type is ArrayType
+                                   && !((ArrayType) param.variable_type).fixed_length && ((ArrayType) param.variable_type).rank == 1) {
+                                       unowned Symbol? array_var = unary.inner.symbol_reference;
+                                       unowned LocalVariable? array_local = array_var as LocalVariable;
+                                       if (array_var != null && array_var.is_internal_symbol ()
+                                           && ((array_local != null && !array_local.captured) || array_var is Field)) {
+                                               ccode.add_assignment (get_array_size_cvalue (unary.inner.target_value), get_array_length_cvalue (unary.inner.target_value, 1));
+                                       }
+                               }
+                       }
+
                        if (unary == null || unary.operator != UnaryOperator.OUT) {
                                continue;
                        }
index 6c74a3186b2b8eae47f6093e5a2f338c7069800c..dcda2cbe05ae436b7674d252d37d0892428d95cc 100644 (file)
@@ -118,6 +118,7 @@ TESTS = \
        methods/contains.vala \
        methods/extern.vala \
        methods/iterator.vala \
+       methods/parameter-ref-array-resize.vala \
        methods/prepostconditions.vala \
        methods/same-name.vala \
        methods/symbolresolution.vala \
diff --git a/tests/methods/parameter-ref-array-resize.vala b/tests/methods/parameter-ref-array-resize.vala
new file mode 100644 (file)
index 0000000..ffe27d3
--- /dev/null
@@ -0,0 +1,11 @@
+void foo (ref int[] a) {
+       a = new int[1];
+}
+
+void main() {
+       int[] a = new int[10 * 1024 * 1024];
+       foo (ref a);
+       for (int i = 1; i < 10 * 1024 * 1024; i++) {
+               a += 4711;
+       }
+}