[libgda] More misc. corrections for bugs discovered by CoverityScan



commit 4a28b714684734cfb35bf38e41931ed4b7efe577
Author: Vivien Malerba <malerba gnome-db org>
Date:   Mon Sep 3 22:01:00 2012 +0200

    More misc. corrections for bugs discovered by CoverityScan

 libgda/gda-quark-list.c                          |    4 +-
 libgda/gda-server-operation.c                    |   12 ++++
 libgda/gda-sql-builder.c                         |    2 +
 libgda/sql-parser/gda-statement-struct.c         |    4 +-
 libgda/sql-parser/lempar.c                       |    3 +-
 providers/reuseable/postgres/gda-postgres-meta.c |    6 ++-
 tools/browser/canvas/browser-canvas-utility.c    |   66 +---------------------
 tools/browser/data-manager/data-source-manager.c |    2 +-
 tools/browser/schema-browser/table-preferences.c |    2 +-
 tools/gda-sql.c                                  |   53 +++++++++++++++--
 tools/web-server.c                               |   52 ++++++++++--------
 11 files changed, 102 insertions(+), 104 deletions(-)
---
diff --git a/libgda/gda-quark-list.c b/libgda/gda-quark-list.c
index 33c2ebc..1a8f2d9 100644
--- a/libgda/gda-quark-list.c
+++ b/libgda/gda-quark-list.c
@@ -146,10 +146,10 @@ protected_value_xor (ProtectedValue *pvalue, gboolean to_clear)
 					break;
 			}
 #ifdef G_OS_WIN32
-			VirtualUnlock (pvalue->cvalue, sizeof (gchar*) * (i + 1));
+			VirtualUnlock (pvalue->cvalue, sizeof (gchar) * (i + 1));
 #else
 #ifdef USE_MLOCK
-			munlock (pvalue->cvalue, sizeof (gchar*) * (i + 1));
+			munlock (pvalue->cvalue, sizeof (gchar) * (i + 1));
 #endif
 #endif
 			free (pvalue->cvalue);
diff --git a/libgda/gda-server-operation.c b/libgda/gda-server-operation.c
index bb22c2f..651cf5a 100644
--- a/libgda/gda-server-operation.c
+++ b/libgda/gda-server-operation.c
@@ -2718,6 +2718,7 @@ gda_server_operation_prepare_create_table (GdaConnection *cnc, const gchar *tabl
 			/* First argument for Column's name */
 			if(!gda_server_operation_set_value_at (op, arg, error, "/FIELDS_A/@COLUMN_NAME/%d", i)){
 				g_object_unref (op);
+				va_end (args);
 				return NULL;
 			}
 
@@ -2727,12 +2728,14 @@ gda_server_operation_prepare_create_table (GdaConnection *cnc, const gchar *tabl
 				g_set_error (error, GDA_SERVER_OPERATION_ERROR, GDA_SERVER_OPERATION_INCORRECT_VALUE_ERROR,
 					     "%s", _("Invalid type"));
 				g_object_unref (op);
+				va_end (args);
 				return NULL;
 			}
 			dbms_type = (gchar *) gda_server_provider_get_default_dbms_type (server,
 											 cnc, type);
 			if (!gda_server_operation_set_value_at (op, dbms_type, error, "/FIELDS_A/@COLUMN_TYPE/%d", i)){
 				g_object_unref (op);
+				va_end (args);
 				return NULL;
 			}
 
@@ -2741,21 +2744,25 @@ gda_server_operation_prepare_create_table (GdaConnection *cnc, const gchar *tabl
 			if (flag & GDA_SERVER_OPERATION_CREATE_TABLE_PKEY_FLAG)
 				if(!gda_server_operation_set_value_at (op, "TRUE", error, "/FIELDS_A/@COLUMN_PKEY/%d", i)){
 					g_object_unref (op);
+					va_end (args);
 					return NULL;
 				}
 			if (flag & GDA_SERVER_OPERATION_CREATE_TABLE_NOT_NULL_FLAG)
 				if(!gda_server_operation_set_value_at (op, "TRUE", error, "/FIELDS_A/@COLUMN_NNUL/%d", i)){
 					g_object_unref (op);
+					va_end (args);
 					return NULL;
 				}
 			if (flag & GDA_SERVER_OPERATION_CREATE_TABLE_AUTOINC_FLAG)
 				if (!gda_server_operation_set_value_at (op, "TRUE", error, "/FIELDS_A/@COLUMN_AUTOINC/%d", i)){
 					g_object_unref (op);
+					va_end (args);
 					return NULL;
 				}
 			if (flag & GDA_SERVER_OPERATION_CREATE_TABLE_UNIQUE_FLAG)
 				if(!gda_server_operation_set_value_at (op, "TRUE", error, "/FIELDS_A/@COLUMN_UNIQUE/%d", i)){
 					g_object_unref (op);
+					va_end (args);
 					return NULL;
 				}
 			if (flag & GDA_SERVER_OPERATION_CREATE_TABLE_FKEY_FLAG) {
@@ -2771,6 +2778,7 @@ gda_server_operation_prepare_create_table (GdaConnection *cnc, const gchar *tabl
 				if (!gda_server_operation_set_value_at (op, fkey_table, error,
 								   "/FKEY_S/%d/FKEY_REF_TABLE", refs)){
 					g_object_unref (op);
+					va_end (args);
 					return NULL;
 				}
 
@@ -2783,6 +2791,7 @@ gda_server_operation_prepare_create_table (GdaConnection *cnc, const gchar *tabl
 					if(!gda_server_operation_set_value_at (op, field, error,
 									   "/FKEY_S/%d/FKEY_FIELDS_A/@FK_FIELD/%d", refs, j)){
 						g_object_unref (op);
+						va_end (args);
 						return NULL;
 					}
 
@@ -2790,6 +2799,7 @@ gda_server_operation_prepare_create_table (GdaConnection *cnc, const gchar *tabl
 					if(!gda_server_operation_set_value_at (op, rfield, error,
 									   "/FKEY_S/%d/FKEY_FIELDS_A/@FK_REF_PK_FIELD/%d", refs, j)){
 						g_object_unref (op);
+						va_end (args);
 						return NULL;
 					}
 				}
@@ -2798,12 +2808,14 @@ gda_server_operation_prepare_create_table (GdaConnection *cnc, const gchar *tabl
 				if (!gda_server_operation_set_value_at (op, fkey_ondelete, error,
 								   "/FKEY_S/%d/FKEY_ONDELETE", refs)){
 					g_object_unref (op);
+					va_end (args);
 					return NULL;
 				}
 				fkey_onupdate = va_arg (args, gchar*);
 				if(!gda_server_operation_set_value_at (op, fkey_onupdate, error,
 								   "/FKEY_S/%d/FKEY_ONUPDATE", refs)){
 					g_object_unref (op);
+					va_end (args);
 					return NULL;
 				}
 			}
diff --git a/libgda/gda-sql-builder.c b/libgda/gda-sql-builder.c
index 2772524..73683c9 100644
--- a/libgda/gda-sql-builder.c
+++ b/libgda/gda-sql-builder.c
@@ -1662,6 +1662,7 @@ gda_sql_builder_add_function (GdaSqlBuilder *builder, const gchar *func_name, ..
 		if (!part) {
 			expr->func->args_list = list;
 			gda_sql_expr_free (expr);
+			va_end (ap);
 			return 0;
 		}
 		list = g_slist_prepend (list, use_part (part, GDA_SQL_ANY_PART (expr->func)));
@@ -1907,6 +1908,7 @@ gda_sql_builder_add_case (GdaSqlBuilder *builder,
 	return add_part (builder, (GdaSqlAnyPart *) expr);
 
  cleanups:
+	va_end (ap);
 	gda_sql_expr_free (expr);
 	return 0;
 }
diff --git a/libgda/sql-parser/gda-statement-struct.c b/libgda/sql-parser/gda-statement-struct.c
index 1094901..42257fb 100644
--- a/libgda/sql-parser/gda-statement-struct.c
+++ b/libgda/sql-parser/gda-statement-struct.c
@@ -198,10 +198,8 @@ gda_sql_statement_type_to_string (GdaSqlStatementType type)
 {
 	GdaSqlStatementContentsInfo *infos;
 	infos = gda_sql_statement_get_contents_infos (type);
-	if (!infos) {
+	if (!infos)
 		return "NONE";
-		TO_IMPLEMENT;
-	}
 	else
 		return infos->name;
 }
diff --git a/libgda/sql-parser/lempar.c b/libgda/sql-parser/lempar.c
index fe56d2d..8d2d6c2 100644
--- a/libgda/sql-parser/lempar.c
+++ b/libgda/sql-parser/lempar.c
@@ -319,9 +319,10 @@ static void yy_destructor(
 */
 static int yy_pop_parser_stack(yyParser *pParser){
   YYCODETYPE yymajor;
-  yyStackEntry *yytos = &pParser->yystack[pParser->yyidx];
+  yyStackEntry *yytos;
 
   if( pParser->yyidx<0 ) return 0;
+  yytos = &pParser->yystack[pParser->yyidx];
 #ifndef NDEBUG
   if( yyTraceFILE && pParser->yyidx>=0 ){
     fprintf(yyTraceFILE,"%sPopping %s\n",
diff --git a/providers/reuseable/postgres/gda-postgres-meta.c b/providers/reuseable/postgres/gda-postgres-meta.c
index 2ad199f..fea38c7 100644
--- a/providers/reuseable/postgres/gda-postgres-meta.c
+++ b/providers/reuseable/postgres/gda-postgres-meta.c
@@ -539,8 +539,10 @@ _gda_postgres_meta__enums (G_GNUC_UNUSED GdaServerProvider *prov, GdaConnection
 	rdata = GDA_POSTGRES_GET_REUSEABLE_DATA (gda_connection_internal_get_provider_data_error (cnc, error));
 	if (!rdata)
 		return FALSE;
+	/*
 	if (rdata->version_float >= 8.3)
-		/*TO_IMPLEMENT*/;
+	        TO_IMPLEMENT;
+	*/
 
 	return TRUE;
 }
@@ -556,8 +558,10 @@ _gda_postgres_meta_enums (G_GNUC_UNUSED GdaServerProvider *prov, GdaConnection *
 	rdata = GDA_POSTGRES_GET_REUSEABLE_DATA (gda_connection_internal_get_provider_data_error (cnc, error));
 	if (!rdata)
 		return FALSE;
+	/*
 	if (rdata->version_float >= 8.3)
 		TO_IMPLEMENT;
+	*/
 
 	return TRUE;
 }
diff --git a/tools/browser/canvas/browser-canvas-utility.c b/tools/browser/canvas/browser-canvas-utility.c
index 0b36ca6..0e7edb7 100644
--- a/tools/browser/canvas/browser-canvas-utility.c
+++ b/tools/browser/canvas/browser-canvas-utility.c
@@ -27,9 +27,6 @@ static gboolean compute_intersect_rect_line (gdouble rectx1, gdouble recty1, gdo
 static void     compute_text_marks_offsets (gdouble x1, gdouble y1, gdouble x2, gdouble y2,
 					    gdouble *xoff, gdouble *yoff, GooCanvasAnchorType *anchor_type);
 
-static GSList *browser_canvas_util_compute_handle_shapes  (GooCanvasItem *parent, GSList *shapes, gint index,
-							 gdouble x1, gdouble y1, gdouble x2, gdouble y2);
-
 static GSList *browser_canvas_canvas_shape_add_to_list (GSList *list, gchar *swallow_id, GooCanvasItem *item);
 static BrowserCanvasCanvasShape *browser_canvas_canvas_shape_find (GSList *list, const gchar *id);
 
@@ -194,13 +191,6 @@ browser_canvas_util_compute_anchor_shapes (GooCanvasItem *parent, GSList *shapes
 				}
 			}
 			
-			/* handle in the middle */
-			if (with_handle)
-				retval = browser_canvas_util_compute_handle_shapes (parent, retval, i, 
-										  points->coords[2], 
-										  points->coords[3], 
-										  points->coords[4], 
-										  points->coords[5]);
 			goo_canvas_points_unref (points);
 		}
 		else {
@@ -344,13 +334,6 @@ browser_canvas_util_compute_anchor_shapes (GooCanvasItem *parent, GSList *shapes
 				}
 			}
 
-			/* handle in the middle */
-			if (with_handle)
-				retval = browser_canvas_util_compute_handle_shapes (parent, retval, i,
-										  points->coords[0], 
-										  points->coords[1], 
-										  points->coords[2], 
-										  points->coords[3]);
 			goo_canvas_points_unref (points);
 		}
 	}
@@ -670,56 +653,9 @@ browser_canvas_util_compute_connect_shapes (GooCanvasItem *parent, GSList *shape
 			retval = browser_canvas_canvas_shape_add_to_list (retval, id, item);
 		}
 	}
-	
-	
-	/* handle in the middle */
-	/*retval = browser_canvas_util_compute_handle_shapes (parent, retval, nb_connect,
-							  points->coords[2], points->coords[3], 
-							  points->coords[4], points->coords[5]);*/
-	goo_canvas_points_unref (points);
-
-	return retval;
-}
 
-/*
- * computes a "handle" in the middle of the 2 points passed as argument*
- * 
- * Warning: the obsolete shapes in @shapes are _not_ removed.
- */
-GSList *
-browser_canvas_util_compute_handle_shapes (GooCanvasItem *parent, GSList *shapes, gint index,
-					 gdouble x1, gdouble y1, gdouble x2, gdouble y2)
-{
-	/* don't add anything... */
-	return shapes;
-
-	GSList *retval = shapes;
-	gdouble x, y, sq = 5.;
-	GooCanvasItem *item;
-	BrowserCanvasCanvasShape *shape;
-	gchar *id;
-
-	/* circle in the middle */
-	x = (x1 + x2) / 2.;
-	y = (y1 + y2) / 2.;
+	goo_canvas_points_unref (points);
 
-	id = g_strdup_printf ("h%d", index);
-	shape = browser_canvas_canvas_shape_find (retval, id);
-	if (shape) {
-		g_object_set (shape->item, 
-			      "center-x", x, "center-y", y, 
-			      NULL);
-		shape->_used = TRUE;
-		g_free (id);
-	}
-	else {
-		item = goo_canvas_ellipse_new (parent, x, y, sq, sq, 
-					       "fill-color", "black",
-					       "visibility", GOO_CANVAS_ITEM_VISIBLE_ABOVE_THRESHOLD,
-					       "visibility-threshold", .9, NULL);
-		retval = browser_canvas_canvas_shape_add_to_list (retval, id, item);
-	}
-	
 	return retval;
 }
 
diff --git a/tools/browser/data-manager/data-source-manager.c b/tools/browser/data-manager/data-source-manager.c
index fad9941..ce6114d 100644
--- a/tools/browser/data-manager/data-source-manager.c
+++ b/tools/browser/data-manager/data-source-manager.c
@@ -575,7 +575,7 @@ data_source_manager_get_sources_array (DataSourceManager *mgr, G_GNUC_UNUSED GEr
 		}
 		else {
 			/* add source to column 0 */
-			array = g_array_new (FALSE, FALSE, sizeof (GArray*));
+			array = g_array_new (FALSE, FALSE, sizeof (gpointer));
 			GArray *subarray = g_array_new (FALSE, FALSE, sizeof (DataSource*));
 			g_array_append_val (array, subarray);
 			g_array_append_val (subarray, source);
diff --git a/tools/browser/schema-browser/table-preferences.c b/tools/browser/schema-browser/table-preferences.c
index 1358b1c..9fc6b49 100644
--- a/tools/browser/schema-browser/table-preferences.c
+++ b/tools/browser/schema-browser/table-preferences.c
@@ -785,7 +785,7 @@ options_form_param_changed_cb (G_GNUC_UNUSED GdauiBasicForm *form, G_GNUC_UNUSED
 static void
 set_preview_widget (TablePreferences *tpref)
 {
-	GtkWidget *preview;
+	GtkWidget *preview = NULL;
 	GtkTreeIter iter;
 
 	if (!tpref->priv->current_column)
diff --git a/tools/gda-sql.c b/tools/gda-sql.c
index fab3a1e..aefb428 100644
--- a/tools/gda-sql.c
+++ b/tools/gda-sql.c
@@ -1373,6 +1373,30 @@ compute_prompt (SqlConsole *console, GString *string, gboolean in_command, Outpu
 }
 
 /*
+ * Check that the @arg string can safely be passed to a shell
+ * to be executed, i.e. it does not contain dangerous things like "rm -rf *"
+ */
+static gboolean
+check_shell_argument (const gchar *arg)
+{
+	const gchar *ptr;
+	g_assert (arg);
+	g_print ("[%s]\n", arg);
+
+	/* check for starting spaces */
+	for (ptr = arg; * ptr && (*ptr == ' '); ptr++);
+	if (!*ptr)
+		return FALSE; /* only spaces is not allowed */
+
+	/* check for the rest */
+	for (; * ptr; ptr++) {
+		if (! isalnum (*ptr) && (*ptr != G_DIR_SEPARATOR))
+			return FALSE;
+	}
+	return TRUE;
+}
+
+/*
  * Change the output file, set to %NULL to be back on stdout
  */
 static gboolean
@@ -1410,12 +1434,22 @@ set_output_file (const gchar *file, GError **error)
 		}
 		else {
 			/* output to a pipe */
-			main_data->output_stream = popen (copy+1, "w");
-			if (!main_data->output_stream) {
+			if (check_shell_argument (copy+1)) {
+				main_data->output_stream = popen (copy+1, "w");
+				if (!main_data->output_stream) {
+					g_set_error (error, TOOLS_ERROR, TOOLS_INTERNAL_COMMAND_ERROR,
+						     _("Can't open pipe '%s': %s"), 
+						     copy,
+						     strerror (errno));
+					g_free (copy);
+					return FALSE;
+				}
+			}
+			else {
 				g_set_error (error, TOOLS_ERROR, TOOLS_INTERNAL_COMMAND_ERROR,
-					     _("Can't open pipe '%s': %s\n"), 
-					     copy,
-					     strerror (errno));
+					     _("Can't open pipe '%s': %s"),
+					     copy + 1,
+					     "program name must only contain alphanumeric characters");
 				g_free (copy);
 				return FALSE;
 			}
@@ -2161,7 +2195,12 @@ output_string (const gchar *str)
 		pager = getenv ("PAGER");
 		if (!pager)
 			pager = "more";
-		pipe = popen (pager, "w");
+		if (!check_shell_argument (pager)) {
+			g_warning ("Invalid PAGER value: must only contain alphanumeric characters");
+			return;
+		}
+		else
+			pipe = popen (pager, "w");
 #ifndef G_OS_WIN32
 		phandler = signal (SIGPIPE, SIG_IGN);
 #endif
@@ -2593,7 +2632,7 @@ build_internal_commands_list (void)
 	c->args = NULL;
 	c->command_func = (GdaInternalCommandFunc) extra_command_set_output;
 	c->user_data = NULL;
-	c->arguments_delimiter_func = NULL;
+	c->arguments_delimiter_func = args_as_string_func;
 	c->unquote_args = TRUE;
 	c->limit_to_main = TRUE;
 	commands->commands = g_slist_prepend (commands->commands, c);
diff --git a/tools/web-server.c b/tools/web-server.c
index ebe44dd..6cef064 100644
--- a/tools/web-server.c
+++ b/tools/web-server.c
@@ -276,35 +276,41 @@ server_callback (G_GNUC_UNUSED SoupServer *server, SoupMessage *msg,
 		gchar **array = NULL;
 		array = g_strsplit (path, "/", 0);
 		
-		const ConnectionSetting *cs;
-		cs = gda_sql_get_connection (array[0]);
+		if (array) {
+			const ConnectionSetting *cs;
+			cs = gda_sql_get_connection (array[0]);
 		
-		if (cs) {
-			if (msg->method == SOUP_METHOD_GET) {
-				ok = get_for_cnc (webserver, msg, cs, array[1] ? &(array[1]) : NULL, &error);
+			if (cs) {
+				if (msg->method == SOUP_METHOD_GET) {
+					ok = get_for_cnc (webserver, msg, cs, array[1] ? &(array[1]) : NULL, &error);
+					done = TRUE;
+				}
+			}
+			else if (!strcmp (path, "~console")) {
+				get_for_console (webserver, msg);
 				done = TRUE;
 			}
-		}
-		else if (!strcmp (path, "~console")) {
-			get_for_console (webserver, msg);
-			done = TRUE;
-		}
-		else if (!strcmp (path, "~irb")) {
-			ok = get_post_for_irb (webserver, msg, cs, query, &error);
-			done = TRUE;
-		}
-		else if (!strcmp (path, "~cnclist")) {
-			get_for_cnclist (webserver, msg, !auth_needed);
-			done = TRUE;
-		}
-		else {
-			if (msg->method == SOUP_METHOD_GET) {
-				ok = get_file (webserver, msg, path, &error);
+			else if (!strcmp (path, "~irb")) {
+				ok = get_post_for_irb (webserver, msg, cs, query, &error);
 				done = TRUE;
 			}
-		}
-		if (array)
+			else if (!strcmp (path, "~cnclist")) {
+				get_for_cnclist (webserver, msg, !auth_needed);
+				done = TRUE;
+			}
+			else {
+				if (msg->method == SOUP_METHOD_GET) {
+					ok = get_file (webserver, msg, path, &error);
+					done = TRUE;
+				}
+			}
+
 			g_strfreev (array);
+		}
+		else {
+			ok= FALSE;
+			done = TRUE;
+		}
 	}
 	
 	if (!ok) {



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