]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
don't allow NULL terminated arrays of value-type structs and remove the
authorRaffaele Sandrini <rasa@gmx.ch>
Thu, 22 Feb 2007 14:01:32 +0000 (14:01 +0000)
committerRaffaele Sandrini <rasa@src.gnome.org>
Thu, 22 Feb 2007 14:01:32 +0000 (14:01 +0000)
2007-02-22  Raffaele Sandrini  <rasa@gmx.ch>

* 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

vala/vala/valacodegenerator.vala

index 0f2b105266000613ea29e83bc352cc5a1efa2c78..6fd5f6f8a2fb8112fc50a01bc34b147979e4d234 100644 (file)
@@ -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 {