]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
codegen: Free intermediate temp-variables of postcondition expression
authorRico Tzschichholz <ricotz@ubuntu.com>
Tue, 9 Mar 2021 06:24:48 +0000 (07:24 +0100)
committerRico Tzschichholz <ricotz@ubuntu.com>
Sat, 13 Mar 2021 20:35:17 +0000 (21:35 +0100)
and not clearing "temp_ref_values" caused invalid references later.

Fixes https://gitlab.gnome.org/GNOME/vala/issues/80

codegen/valaccodebasemodule.vala
tests/Makefile.am
tests/methods/postconditions-temp-variables.vala [new file with mode: 0644]
tests/methods/preconditions-temp-variables.vala [new file with mode: 0644]

index 15ae9e4fcab65cc3829dbac3e72568976d76e2fe..539630901d9ba84a9fc1247d39cec478906836e0 100644 (file)
@@ -6488,6 +6488,12 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
                requires_assert = true;
 
                ccode.add_expression (cassert);
+
+               foreach (var value in temp_ref_values) {
+                       ccode.add_expression (destroy_value (value));
+               }
+
+               temp_ref_values.clear ();
        }
 
        public unowned DataType? get_this_type () {
index 4c97b72a739e486b0d8a38f2d43b8b993a7a52b1..2c0724e38b98cd5710746ec7e98818761d8fc039 100644 (file)
@@ -168,9 +168,11 @@ TESTS = \
        methods/parameter-ref-array-resize-captured.vala \
        methods/parameter-ref-delegate.vala \
        methods/parameter-ref-element-access.vala \
+       methods/preconditions-temp-variables.vala \
        methods/prepostconditions.vala \
        methods/prepostconditions-captured.vala \
        methods/postconditions.vala \
+       methods/postconditions-temp-variables.vala \
        methods/return-unowned-delegate.vala \
        methods/same-name.vala \
        methods/symbolresolution.vala \
diff --git a/tests/methods/postconditions-temp-variables.vala b/tests/methods/postconditions-temp-variables.vala
new file mode 100644 (file)
index 0000000..4641472
--- /dev/null
@@ -0,0 +1,17 @@
+string foo () ensures (result.to_string () != "23") {
+       return 4711.to_string ();
+}
+
+string bar (bool b) ensures (result.to_string () != "4711") {
+       if (b) {
+               return 23.to_string ();
+       } else {
+               return 42.to_string ();
+       }
+}
+
+void main () {
+       assert (foo () == "4711");
+       assert (bar (true) == "23");
+       assert (bar (false) == "42");
+}
diff --git a/tests/methods/preconditions-temp-variables.vala b/tests/methods/preconditions-temp-variables.vala
new file mode 100644 (file)
index 0000000..b116166
--- /dev/null
@@ -0,0 +1,8 @@
+string foo (int i) requires (i.to_string () == "23" || i.to_string () == "42") {
+       return i.to_string ();
+}
+
+void main () {
+       assert (foo (23) == "23");
+       assert (foo (42) == "42");
+}