[vala/0.48] codegen: Free intermediate temp-variables of postcondition expression



commit 1c326371c1816d205009aee5e712aa11cec0a56c
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Mar 9 07:24:48 2021 +0100

    codegen: Free intermediate temp-variables of postcondition expression
    
    and not clearing "temp_ref_values" caused invalid references later.
    
    Fixes https://gitlab.gnome.org/GNOME/vala/issues/80

 codegen/valaccodebasemodule.vala                 |  6 ++++++
 tests/Makefile.am                                |  2 ++
 tests/methods/postconditions-temp-variables.vala | 17 +++++++++++++++++
 tests/methods/preconditions-temp-variables.vala  |  8 ++++++++
 4 files changed, 33 insertions(+)
---
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 60c4d34cd..c94c41ee2 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -6461,6 +6461,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 () {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index d4b4fef16..a1d898a3f 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -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
index 000000000..46414725d
--- /dev/null
+++ b/tests/methods/postconditions-temp-variables.vala
@@ -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
index 000000000..b116166bb
--- /dev/null
+++ b/tests/methods/preconditions-temp-variables.vala
@@ -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");
+}


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