Browse Source

Fix usage of shell_[un]escape functions.

These functions returns newly allocated string, but calling don't freeze correctly allocated memory.
Slava Zanko 16 years ago
parent
commit
1a899905bd
4 changed files with 40 additions and 13 deletions
  1. 4 3
      src/command.c
  2. 21 6
      src/complete.c
  3. 5 0
      src/file.c
  4. 10 4
      vfs/fish.c

+ 4 - 3
src/command.c

@@ -59,15 +59,15 @@ WInput *cmdline;
  * they want the behavior they are used to in the shell.
  */
 static int
-examine_cd (char *path)
+examine_cd (const char *_path)
 {
     int result, qlen;
-    char *path_tilde;
+    char *path_tilde, *path;
     char *p, *q, *r, *s, c;
     const char *t;
 
     /* Tilde expansion */
-    path = shell_unescape(path);
+    path = shell_unescape(_path);
     path_tilde = tilde_expand (path);
 
     /* Leave space for further expansion */
@@ -139,6 +139,7 @@ examine_cd (char *path)
     }
     g_free (q);
     g_free (path_tilde);
+    g_free (path);
     return result;
 }
 

+ 21 - 6
src/complete.c

@@ -73,8 +73,9 @@ static const char * show_c_flags(INPUT_COMPLETE_FLAGS flags)
 #endif /* DO_CMPLETION_DEBUG */
 
 static char *
-filename_completion_function (char *text, int state, INPUT_COMPLETE_FLAGS flags)
+filename_completion_function (const char *_text, int state, INPUT_COMPLETE_FLAGS flags)
 {
+    char *text;
     static DIR *directory;
     static char *filename = NULL;
     static char *dirname = NULL;
@@ -87,7 +88,7 @@ filename_completion_function (char *text, int state, INPUT_COMPLETE_FLAGS flags)
     SHOW_C_CTX("filename_completion_function");
 
     if (text && (flags & INPUT_COMPLETE_SHELL_ESC))
-        text = shell_unescape (text);
+        text = shell_unescape (_text);
 
     /* If we're starting the match process, initialize us a bit. */
     if (!state){
@@ -214,8 +215,11 @@ filename_completion_function (char *text, int state, INPUT_COMPLETE_FLAGS flags)
 
 	if (temp && (flags & INPUT_COMPLETE_SHELL_ESC))
 	{
+	    char *temp2 = temp;
 	    temp = shell_escape(temp);
+	    g_free(temp2);
 	}
+	g_free(text);
 	return temp;
     }
 }
@@ -448,8 +452,9 @@ hostname_completion_function (char *text, int state, INPUT_COMPLETE_FLAGS flags)
  * table of shell built-ins.
  */
 static char *
-command_completion_function (char *text, int state, INPUT_COMPLETE_FLAGS flags)
+command_completion_function (const char *_text, int state, INPUT_COMPLETE_FLAGS flags)
 {
+    char *text;
     static const char *path_end;
     static int isabsolute;
     static int phase;
@@ -478,8 +483,7 @@ command_completion_function (char *text, int state, INPUT_COMPLETE_FLAGS flags)
 
     if (!(flags & INPUT_COMPLETE_COMMANDS))
         return 0;
-
-    text = shell_unescape(text);
+    text = shell_unescape(_text);
     flags &= ~INPUT_COMPLETE_SHELL_ESC;
 
     if (!state) {		/* Initialize us a little bit */
@@ -499,10 +503,16 @@ command_completion_function (char *text, int state, INPUT_COMPLETE_FLAGS flags)
     }
 
     if (isabsolute) {
+	char *temp_p;
 	p = filename_completion_function (text, state, flags);
-	if (!p)
+	if (!p){
+	    g_free(text);
 	    return 0;
+	}
+	temp_p = p;
 	p = shell_escape(p);
+	g_free(temp_p);
+	g_free(text);
 	return p;
     }
 
@@ -554,14 +564,19 @@ command_completion_function (char *text, int state, INPUT_COMPLETE_FLAGS flags)
     if (!found) {
 	g_free (path);
 	path = NULL;
+	g_free(text);
 	return NULL;
     }
     if ((p = strrchr (found, PATH_SEP)) != NULL) {
 	p++;
+	g_free(found);
+	found = p;
 	p = shell_escape(p);
 	g_free(found);
+	g_free(text);
 	return p;
     }
+    g_free(text);
     return found;
 
 }

+ 5 - 0
src/file.c

@@ -2118,9 +2118,14 @@ panel_operate (void *source_panel, FileOperation operation,
 		    value = transform_error;
 		else {
 		    char *temp2 = concat_dir_and_file (dest, temp);
+		    char *temp3;
 
+		    temp3 = source_with_path;
 		    source_with_path = shell_unescape(source_with_path);
+		    g_free(temp3);
+		    temp3 = temp2;
 		    temp2 = shell_unescape(temp2);
+		    g_free(temp3);
 
 		    switch (operation) {
 		    case OP_COPY:

+ 10 - 4
vfs/fish.c

@@ -508,6 +508,7 @@ fish_dir_load(struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
 
 	switch(buffer[0]) {
 	case ':': {
+	    char *temp;
 	    char *data_start = buffer+1;
 	    char *filename = data_start;
 	    char *linkname = data_start;
@@ -542,10 +543,14 @@ fish_dir_load(struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
 			}
 
 			ent->name = str_dup_range(filename, filename_bound);
-			shell_unescape(ent->name);
+			temp = ent->name;
+			ent->name = shell_unescape(ent->name);
+			g_free(temp);
 
 			ent->ino->linkname = str_dup_range(linkname, linkname_bound);
-			shell_unescape(ent->ino->linkname);
+			temp = ent->ino->linkname;
+			ent->ino->linkname = shell_unescape(ent->ino->linkname);
+			g_free(temp);
 		} else {
 			/* we expect: "escaped-name" */
 			if (filename_bound - filename > 2)
@@ -559,9 +564,10 @@ fish_dir_load(struct vfs_class *me, struct vfs_s_inode *dir, char *remote_path)
 				if (*(filename_bound - 1) == '"')
 					--filename_bound;
 			}
-
 			ent->name = str_dup_range(filename, filename_bound);
-			shell_unescape(ent->name);
+			temp = ent->name;
+			ent->name = shell_unescape(ent->name);
+			g_free(temp);
 		}
 		break;
 	}