vala r942 - in trunk: . ccode gobject



Author: juergbi
Date: Fri Feb  1 16:59:58 2008
New Revision: 942
URL: http://svn.gnome.org/viewvc/vala?rev=942&view=rev

Log:
2008-02-01  Juerg Billeter  <j bitron ch>

	* ccode/valaccodevariabledeclarator.vala,
	  gobject/valaccodegenerator.vala: 0-initialize temporary struct
	  variables to avoid passing uninitialized structs by reference,
	  fixes bug 513144


Modified:
   trunk/ChangeLog
   trunk/ccode/valaccodevariabledeclarator.vala
   trunk/gobject/valaccodegenerator.vala

Modified: trunk/ccode/valaccodevariabledeclarator.vala
==============================================================================
--- trunk/ccode/valaccodevariabledeclarator.vala	(original)
+++ trunk/ccode/valaccodevariabledeclarator.vala	Fri Feb  1 16:59:58 2008
@@ -1,6 +1,6 @@
 /* valaccodevariabledeclarator.vala
  *
- * Copyright (C) 2006-2007  JÃrg Billeter
+ * Copyright (C) 2006-2008  JÃrg Billeter
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -56,10 +56,16 @@
 
 	public override void write_declaration (CCodeWriter! writer) {
 		writer.write_string (name);
+
+		// initializer lists can't be moved to a separate statement
+		if (initializer is CCodeInitializerList) {
+			writer.write_string (" = ");
+			initializer.write (writer);
+		}
 	}
 
 	public override void write_initialization (CCodeWriter! writer) {
-		if (initializer != null) {
+		if (initializer != null && !(initializer is CCodeInitializerList)) {
 			writer.write_indent (line);
 
 			writer.write_string (name);

Modified: trunk/gobject/valaccodegenerator.vala
==============================================================================
--- trunk/gobject/valaccodegenerator.vala	(original)
+++ trunk/gobject/valaccodegenerator.vala	Fri Feb  1 16:59:58 2008
@@ -854,17 +854,11 @@
 				if (decl.type_reference.data_type.get_default_value () != null) {
 					((CCodeVariableDeclarator) decl.ccodenode).initializer = new CCodeConstant (decl.type_reference.data_type.get_default_value ());
 				} else {
-					var st = (Struct) decl.type_reference.data_type;
+					// 0-initialize struct with struct initializer { 0 }
+					var clist = new CCodeInitializerList ();
+					clist.append (new CCodeConstant ("0"));
 
-					/* memset needs string.h */
-					string_h_needed = true;
-
-					var czero = new CCodeFunctionCall (new CCodeIdentifier ("memset"));
-					czero.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier (get_variable_cname (decl.name))));
-					czero.add_argument (new CCodeConstant ("0"));
-					czero.add_argument (new CCodeIdentifier ("sizeof (%s)".printf (decl.type_reference.get_cname ())));
-
-					cfrag.append (new CCodeExpressionStatement (czero));
+					((CCodeVariableDeclarator) decl.ccodenode).initializer = clist;
 				}
 			}
 		}
@@ -1187,9 +1181,18 @@
 			// sets #line
 			decl.ccodenode = vardecl;
 			cdecl.add_declarator (vardecl);
-			
+
+			var st = decl.type_reference.data_type as Struct;
+
 			if (decl.type_reference.data_type != null && decl.type_reference.data_type.is_reference_type ()) {
 				vardecl.initializer = new CCodeConstant ("NULL");
+			} else if (st != null && !st.is_simple_type ()) {
+				// 0-initialize struct with struct initializer { 0 }
+				// necessary as they will be passed by reference
+				var clist = new CCodeInitializerList ();
+				clist.append (new CCodeConstant ("0"));
+
+				vardecl.initializer = clist;
 			}
 			
 			cfrag.append (cdecl);



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