[vala] codegen: check for null value before calling g_strv_length



commit 265d5d2f7a140652ca18446d8eec63ca10510131
Author: Luca Bruno <lucabru src gnome org>
Date:   Tue Jun 23 13:26:19 2015 +0200

    codegen: check for null value before calling g_strv_length
    
    Fixes bug 751338

 codegen/valagobjectmodule.vala |    5 ++++-
 tests/Makefile.am              |    1 +
 tests/objects/bug751338.vala   |   27 +++++++++++++++++++++++++++
 3 files changed, 32 insertions(+), 1 deletions(-)
---
diff --git a/codegen/valagobjectmodule.vala b/codegen/valagobjectmodule.vala
index 79629af..cc14136 100644
--- a/codegen/valagobjectmodule.vala
+++ b/codegen/valagobjectmodule.vala
@@ -333,9 +333,12 @@ public class Vala.GObjectModule : GTypeModule {
                                ccode.add_assignment (new CCodeIdentifier ("boxed"), cgetcall);
                                ccall.add_argument (new CCodeIdentifier ("boxed"));
 
+                               var cisnull = new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, new 
CCodeIdentifier ("boxed"), new CCodeConstant ("NULL"));
                                var cstrvlen = new CCodeFunctionCall (new CCodeIdentifier ("g_strv_length"));
                                cstrvlen.add_argument (new CCodeIdentifier ("boxed"));
-                               ccall.add_argument (cstrvlen);
+                               var ccond = new CCodeConditionalExpression (cisnull, new CCodeConstant ("0"), 
cstrvlen);
+
+                               ccall.add_argument (ccond);
                                ccode.add_expression (ccall);
                                ccode.close ();
                        } else {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 03b228d..1666826 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -164,6 +164,7 @@ TESTS = \
        objects/bug701978.vala \
        objects/bug702736.vala \
        objects/bug702846.vala \
+       objects/bug751338.vala \
        errors/errors.vala \
        errors/bug567181.vala \
        errors/bug579101.vala \
diff --git a/tests/objects/bug751338.vala b/tests/objects/bug751338.vala
new file mode 100644
index 0000000..04bacac
--- /dev/null
+++ b/tests/objects/bug751338.vala
@@ -0,0 +1,27 @@
+public class Foo : Object {
+       public string[]? strings {
+               get { return this._strings; }
+               set { this._strings = value; }
+       }
+
+       private string[]? _strings;
+}
+
+void main() {
+       string[]? strings;
+       var f = new Foo();
+       
+       f.set("strings", new string[]{ "foo", "bar" });
+       f.get("strings", out strings);
+       assert (strings[0] == "foo");
+       assert (strings[1] == "bar");
+       
+       f.set("strings", null);
+       f.get("strings", out strings);
+       assert(strings == null);
+       
+       f.set("strings", new string[]{ "foo", "bar" });
+       f.get("strings", out strings);
+       assert (strings[0] == "foo");
+       assert (strings[1] == "bar");
+}


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