From: Raffaele Sandrini Date: Thu, 22 Feb 2007 14:01:32 +0000 (+0000) Subject: don't allow NULL terminated arrays of value-type structs and remove the X-Git-Tag: VALA_0_0_6~15 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4194759ccc38eb74c2cd30b4c14717024a977388;p=thirdparty%2Fvala.git don't allow NULL terminated arrays of value-type structs and remove the 2007-02-22 Raffaele Sandrini * vala/valacodegenerator.vala: don't allow NULL terminated arrays of value-type structs and remove the check on such arrays in a freach statement svn path=/trunk/; revision=192 --- diff --git a/vala/vala/valacodegenerator.vala b/vala/vala/valacodegenerator.vala index 0f2b10526..6fd5f6f8a 100644 --- a/vala/vala/valacodegenerator.vala +++ b/vala/vala/valacodegenerator.vala @@ -2201,6 +2201,7 @@ public class Vala.CodeGenerator : CodeVisitor { var array_len = get_array_length_cexpression (stmt.collection, 1); + /* the array has no length parameter i.e. is NULL-terminated array */ if (array_len is CCodeConstant) { var it_name = "%s_it".printf (stmt.variable_name); @@ -2224,6 +2225,7 @@ public class Vala.CodeGenerator : CodeVisitor { cfor.add_iterator (new CCodeAssignment (new CCodeIdentifier (it_name), new CCodeBinaryExpression (CCodeBinaryOperator.PLUS, new CCodeIdentifier (it_name), new CCodeConstant ("1")))); cblock.add_statement (cfor); + /* the array has a length parameter */ } else { var it_name = (stmt.variable_name + "_it"); @@ -2243,11 +2245,17 @@ public class Vala.CodeGenerator : CodeVisitor { var ccond_ind2 = new CCodeBinaryExpression (CCodeBinaryOperator.LESS_THAN, new CCodeIdentifier (it_name), array_len); var ccond_ind = new CCodeBinaryExpression (CCodeBinaryOperator.AND, ccond_ind1, ccond_ind2); - var ccond_term1 = new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, array_len, new CCodeConstant ("-1")); - var ccond_term2 = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeElementAccess (new CCodeIdentifier (collection_backup.name), new CCodeIdentifier (it_name)), new CCodeConstant ("NULL")); - var ccond_term = new CCodeBinaryExpression (CCodeBinaryOperator.AND, ccond_term1, ccond_term2); - - var ccond = new CCodeBinaryExpression (CCodeBinaryOperator.OR, new CCodeParenthesizedExpression (ccond_ind), new CCodeParenthesizedExpression (ccond_term)); + /* only check for null if the containers elements are of reference-type */ + CCodeBinaryExpression ccond; + if (arr.element_type.is_reference_type ()) { + var ccond_term1 = new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, array_len, new CCodeConstant ("-1")); + var ccond_term2 = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeElementAccess (new CCodeIdentifier (collection_backup.name), new CCodeIdentifier (it_name)), new CCodeConstant ("NULL")); + var ccond_term = new CCodeBinaryExpression (CCodeBinaryOperator.AND, ccond_term1, ccond_term2); + + ccond = new CCodeBinaryExpression (CCodeBinaryOperator.OR, new CCodeParenthesizedExpression (ccond_ind), new CCodeParenthesizedExpression (ccond_term)); + } else { + ccond = ccond_ind; + } var cfor = new CCodeForStatement (ccond, cbody); cfor.add_initializer (new CCodeAssignment (new CCodeIdentifier (it_name), new CCodeConstant ("0"))); @@ -2814,6 +2822,17 @@ public class Vala.CodeGenerator : CodeVisitor { } } + /* if we reach this point we were not able to get the explicit length of the array + * this is not allowed for an array of non-reference-type structs + */ + if (((Array)array_expr.static_type.data_type).element_type is Struct) { + var s = (Struct)((Array)array_expr.static_type.data_type).element_type; + if (!s.is_reference_type ()) { + array_expr.error = true; + Report.error (array_expr.source_reference, "arrays of value-type structs with no explicit length parameter are not supported"); + } + } + if (!is_out) { return new CCodeConstant ("-1"); } else {