Browse Source

Fix segmentation while wrong XML file

Serj Kalichev 3 years ago
parent
commit
7035b1b2b8
2 changed files with 9 additions and 8 deletions
  1. 3 2
      bin/clish.c
  2. 6 6
      clish/shell/shell_xml.c

+ 3 - 2
bin/clish.c

@@ -75,8 +75,8 @@ int main(int argc, char **argv)
 	bool_t istimeout = BOOL_FALSE;
 	unsigned int timeout = 0;
 	bool_t cmd = BOOL_FALSE; /* -c option */
-	lub_list_t *cmds; /* Commands defined by -c */
-	lub_list_node_t *iter;
+	lub_list_t *cmds = NULL; /* Commands defined by -c */
+	lub_list_node_t *iter = NULL;
 	const char *histfile = "~/.clish_history";
 	char *histfile_expanded = NULL;
 	unsigned int histsize = 50;
@@ -264,6 +264,7 @@ int main(int argc, char **argv)
 	clish_xmldoc_start();
 	if (clish_shell_load_scheme(shell, xml_path, xslt_file))
 		goto end;
+
 	/* Set communication to the konfd */
 	clish_shell__set_socket(shell, socket_path);
 	/* Set lockless mode */

+ 6 - 6
clish/shell/shell_xml.c

@@ -85,12 +85,12 @@ static int process_node(clish_shell_t *shell, clish_xmlnode_t *node,
 int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path, const char *xslt_path)
 {
 	const char *path = xml_path;
-	char *buffer;
-	char *dirname;
+	char *buffer = NULL;
+	char *dirname = NULL;
 	char *saveptr = NULL;
 	int res = -1;
 	clish_xmldoc_t *doc = NULL;
-	DIR *dir;
+	DIR *dir = NULL;
 
 #ifdef HAVE_LIB_LIBXSLT
 	clish_xslt_t *xslt = NULL;
@@ -116,7 +116,7 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path, const cha
 	/* Loop though each directory */
 	for (dirname = strtok_r(buffer, path_separators, &saveptr);
 		dirname; dirname = strtok_r(NULL, path_separators, &saveptr)) {
-		struct dirent *entry;
+		struct dirent *entry = NULL;
 
 		/* Search this directory for any XML files */
 		dir = opendir(dirname);
@@ -184,6 +184,7 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path, const cha
 			root = clish_xmldoc_get_root(doc);
 			r = process_node(this, root, NULL);
 			clish_xmldoc_release(doc);
+			doc = NULL;
 
 			/* Error message */
 			if (r) {
@@ -195,6 +196,7 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path, const cha
 			lub_string_free(filename);
 		}
 		closedir(dir);
+		dir = NULL;
 	}
 
 /* To don't free memory twice on cleanup */
@@ -202,8 +204,6 @@ int clish_shell_load_scheme(clish_shell_t *this, const char *xml_path, const cha
 	if (!xslt_path)
 		xslt = NULL;
 #endif
-	doc = NULL;
-	dir = NULL;
 	res = 0; /* Success */
 error:
 	lub_string_free(buffer);