[PATCH] Initialize variable for safety and to avoid warnings.

Greg Troxel gdt at ir.bbn.com
Wed Jun 30 05:12:23 PDT 2010


In gconf_value_to_value, initialize gvalue to NULL, because glib
documentation says not to rely on runtime warning checks and because
compilers might not infer that gvalue is always initialized on return.
If glib checks are enabled, this changed will have no runtime effect
because the checks already return NULL.
---
 src/main.c |   13 +++++++++++++
 1 files changed, 13 insertions(+), 0 deletions(-)

diff --git a/src/main.c b/src/main.c
index adbe8a8..ffd4ac5 100644
--- a/src/main.c
+++ b/src/main.c
@@ -53,6 +53,19 @@ gconf_value_to_value (GConfValue *value)
 	g_return_val_if_fail (value->type == GCONF_VALUE_STRING ||
 			      value->type == GCONF_VALUE_INT, NULL);
 
+	/* Initialize gvalue for two reasons:
+	 * 1. Avoid compiler warnings about use of uninitialized
+	 *    values for compilers that do not infer from the default
+	 *    expansion of the above macro that type is STRING or INT.
+	 * 2. Protect against the case that the above warnings are
+	 *    disabled, causing the function to return NULL instead of
+	 *    an undefined value.
+	 * (Note that compilers that can infer that gvalue would never
+	 * be used uninitialized can be expected to optimize out this
+	 * statement.)
+	 */
+	gvalue = NULL;
+
 	if (value->type == GCONF_VALUE_STRING) {
 		const char *str;
 
-- 
1.7.0.5



More information about the GeoClue mailing list