From: Luca Bruno Date: Tue, 23 Jun 2015 11:26:19 +0000 (+0200) Subject: codegen: check for null value before calling g_strv_length X-Git-Tag: 0.29.3~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=265d5d2f7a140652ca18446d8eec63ca10510131;p=thirdparty%2Fvala.git codegen: check for null value before calling g_strv_length Fixes bug 751338 --- diff --git a/codegen/valagobjectmodule.vala b/codegen/valagobjectmodule.vala index 79629afa3..cc1413639 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 03b228d67..1666826cb 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 000000000..04bacac00 --- /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"); +}