[anjuta] am-project: Avoid a crash when adding a target to an invalid group



commit 6058fbca565430ff00db2c267a65dbd189315a5c
Author: SÃbastien Granjoux <seb sfo free fr>
Date:   Fri Mar 30 17:08:57 2012 +0200

    am-project: Avoid a crash when adding a target to an invalid group
    
    You can get such invalid group by load a project with an empty configure.ac and Makefile.am file.

 plugins/am-project/amp-node.c      |    2 +-
 plugins/am-project/amp-target.c    |   15 ++++++++++++++-
 plugins/am-project/amp-target.h    |    2 +-
 plugins/am-project/projectparser.c |   33 +++++++++++++++++++++++++++------
 plugins/am-project/tests/empty.at  |   17 +++++++++++++++++
 5 files changed, 60 insertions(+), 9 deletions(-)
---
diff --git a/plugins/am-project/amp-node.c b/plugins/am-project/amp-node.c
index 89fc471..7fbcc5a 100644
--- a/plugins/am-project/amp-node.c
+++ b/plugins/am-project/amp-node.c
@@ -99,7 +99,7 @@ amp_node_new_valid(AnjutaProjectNode *parent, AnjutaProjectNodeType type, GFile
 			node = ANJUTA_PROJECT_NODE (amp_group_node_new_valid (file, FALSE, error));
 			break;
 		case ANJUTA_PROJECT_TARGET:
-			node = ANJUTA_PROJECT_NODE (amp_target_node_new_valid (name, type, NULL, 0, error));
+			node = ANJUTA_PROJECT_NODE (amp_target_node_new_valid (name, type, NULL, 0, parent, error));
 			break;
 		case ANJUTA_PROJECT_OBJECT:
 			node = ANJUTA_PROJECT_NODE (amp_object_node_new_valid (file, type, error));
diff --git a/plugins/am-project/amp-target.c b/plugins/am-project/amp-target.c
index 252dc2f..71bfbfc 100644
--- a/plugins/am-project/amp-target.c
+++ b/plugins/am-project/amp-target.c
@@ -328,10 +328,23 @@ amp_target_node_new (const gchar *name, AnjutaProjectNodeType type, const gchar
 }
 
 AmpTargetNode*
-amp_target_node_new_valid (const gchar *name, AnjutaProjectNodeType type, const gchar *install, gint flags, GError **error)
+amp_target_node_new_valid (const gchar *name, AnjutaProjectNodeType type, const gchar *install, gint flags, AnjutaProjectNode *parent, GError **error)
 {
 	const gchar *basename;
 
+	/* Check parent if present */
+	if (parent != NULL)
+	{
+		if ((anjuta_project_node_get_node_type (parent) == ANJUTA_PROJECT_GROUP) &&
+		    (amp_group_node_get_makefile_token (AMP_GROUP_NODE (parent)) == NULL))
+		{
+			amp_set_error (error, IANJUTA_PROJECT_ERROR_VALIDATION_FAILED,
+		               _("Target parent is not a valid group"));
+
+			return NULL;
+		}
+	}
+
 	/* Validate target name */
 	if (!name || strlen (name) <= 0)
 	{
diff --git a/plugins/am-project/amp-target.h b/plugins/am-project/amp-target.h
index 342605c..9d674f8 100644
--- a/plugins/am-project/amp-target.h
+++ b/plugins/am-project/amp-target.h
@@ -69,7 +69,7 @@ void amp_target_node_set_type (AmpTargetNode *target, AmTokenType type);
 AnjutaTokenType amp_target_node_get_first_token_type (AmpTargetNode *target);
 AnjutaTokenType amp_target_node_get_next_token_type (AmpTargetNode *target, AnjutaTokenType type);
 AmpTargetNode* amp_target_node_new (const gchar *name, AnjutaProjectNodeType type, const gchar *install, gint flags);
-AmpTargetNode* amp_target_node_new_valid (const gchar *name, AnjutaProjectNodeType type, const gchar *install, gint flags, GError **error);
+AmpTargetNode* amp_target_node_new_valid (const gchar *name, AnjutaProjectNodeType type, const gchar *install, gint flags, AnjutaProjectNode *parent, GError **error);
 void amp_target_node_free (AmpTargetNode *node);
 void amp_target_node_update_node (AmpTargetNode *node, AmpTargetNode *new_node);
 void amp_target_changed (AmpTargetNode *node);
diff --git a/plugins/am-project/projectparser.c b/plugins/am-project/projectparser.c
index e0af3f9..988d992 100644
--- a/plugins/am-project/projectparser.c
+++ b/plugins/am-project/projectparser.c
@@ -70,6 +70,25 @@ print (const gchar *message, ...)
 	fputc('\n', output_stream);
 }
 
+static void
+print_error (const gchar *message, ...)
+{
+	va_list args;
+
+	va_start (args, message);
+	if ((output_stream == NULL) || (output_stream == stdout))
+	{
+		vfprintf (stderr, message, args);
+		fputc('\n', stderr);
+	}
+	else
+	{
+		vfprintf (output_stream, message, args);
+		fputc('\n', output_stream);
+	}
+	va_end (args);
+}
+
 static void list_children (IAnjutaProject *project, AnjutaProjectNode *root, AnjutaProjectNode *parent, gint indent, const gchar *path);
 
 static void
@@ -510,6 +529,8 @@ main(int argc, char *argv[])
 		exit (1);
 	}
 
+	open_output ();
+
 	/* Execute commands */
 	for (command = &argv[1]; *command != NULL; command++)
 	{
@@ -538,7 +559,7 @@ main(int argc, char *argv[])
 
 				if (best == 0)
 				{
-					fprintf (stderr, "Error: No backend for loading project in %s\n", *command);
+					print_error ("Error: No backend for loading project in %s", *command);
 					break;
 				}
 				else
@@ -586,7 +607,7 @@ main(int argc, char *argv[])
 			}
 			else
 			{
-				fprintf (stderr, "Error: unknown command %s %s\n", *command, command[1]);
+				print_error ("Error: unknown command %s %s", *command, command[1]);
 				break;
 			}
 		}
@@ -694,7 +715,7 @@ main(int argc, char *argv[])
 			}
 			else
 			{
-				fprintf (stderr, "Error: unknown command %s\n", *command);
+				print_error ("Error: unknown command %s", *command);
 
 				break;
 			}
@@ -734,17 +755,17 @@ main(int argc, char *argv[])
 		}
 		else
 		{
-			fprintf (stderr, "Error: unknown command %s\n", *command);
+			print_error ("Error: unknown command %s", *command);
 
 			break;
 		}
 		amp_project_wait_ready (project);
 		if (error != NULL)
 		{
-			fprintf (stderr, "Error: %s\n", error->message == NULL ? "unknown error" : error->message);
+			print_error ("Error: %s", error->message == NULL ? "unknown error" : error->message);
 
 			g_error_free (error);
-			break;
+			error = NULL;
 		}
 	}
 
diff --git a/plugins/am-project/tests/empty.at b/plugins/am-project/tests/empty.at
index 91cec10..735112e 100644
--- a/plugins/am-project/tests/empty.at
+++ b/plugins/am-project/tests/empty.at
@@ -37,4 +37,21 @@ AT_CHECK([diff output expect])
 
 
 
+AS_MKDIR_P([empty4])
+AT_DATA([empty4/configure.ac],
+[[]])
+AT_DATA([empty4/Makefile.am],
+[[]])
+AT_DATA([expect],
+[[Error: Target parent is not a valid group
+ROOT (): empty4
+    SOURCE (): configure.ac
+]])
+AT_PARSER_CHECK([load empty4 \
+		 add target 0 target1 prog \
+		 list])
+AT_CHECK([diff output expect])
+
+
+
 AT_CLEANUP



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