Re: Patch for cmd.c



Hi,
> 
> this patch fixes rare and almost impossible off-by-one.
> If user input is empty (""), these lines can do very bad thing:
> 
>     if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){
>         dirflag = 1;
>         reg_exp_t [strlen(reg_exp_t) - 1] = 0;
>     }
> 
> It is almoust impossible to meet PATH_SEP before '\0', but sometimes it happens.

Patch attached, sorry for inconvenience.

-- 
Regards,
Andrew V. Samoilov.
src/ChangeLog:

	* cmd.c (select_unselect_cmd): Collect repeated code from
	(un)?select_cmd here.  Fix rare off-by-one and memory leak if
	empty pattern passed.

	(select_cmd): Use select_unselect_cmd().
	(unselect_cmd): Likewise.

--- cmd.c~	Mon Sep 27 09:21:04 2004
+++ cmd.c	Fri Oct 22 21:55:28 2004
@@ -471,95 +471,66 @@ void reverse_selection_cmd (void)
     }
 }
 
-void select_cmd (void)
+static void
+select_unselect_cmd (const char *title, int cmd)
 {
     char *reg_exp, *reg_exp_t;
     int i;
     int c;
     int dirflag = 0;
 
-    reg_exp = input_dialog (_(" Select "), "", easy_patterns ? "*" : ".");
+    reg_exp = input_dialog (title, "", easy_patterns ? "*" : ".");
     if (!reg_exp)
 	return;
-    
+    if (!*reg_exp) {
+	g_free (reg_exp);
+	return;
+    }
+
     reg_exp_t = reg_exp;
 
     /* Check if they specified a directory */
-    if (*reg_exp_t == PATH_SEP){
-        dirflag = 1;
-        reg_exp_t++;
-    }
-    if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){
-        dirflag = 1;
-        reg_exp_t [strlen(reg_exp_t) - 1] = 0;
+    if (*reg_exp_t == PATH_SEP) {
+	dirflag = 1;
+	reg_exp_t++;
+    }
+    if (reg_exp_t[strlen (reg_exp_t) - 1] == PATH_SEP) {
+	dirflag = 1;
+	reg_exp_t[strlen (reg_exp_t) - 1] = 0;
     }
 
-    for (i = 0; i < current_panel->count; i++){
-        if (!strcmp (current_panel->dir.list [i].fname, ".."))
-            continue;
-	if (S_ISDIR (current_panel->dir.list [i].st.st_mode)){
+    for (i = 0; i < current_panel->count; i++) {
+	if (!strcmp (current_panel->dir.list[i].fname, ".."))
+	    continue;
+	if (S_ISDIR (current_panel->dir.list[i].st.st_mode)) {
 	    if (!dirflag)
-                continue;
-        } else {
-            if (dirflag)
-                continue;
+		continue;
+	} else {
+	    if (dirflag)
+		continue;
 	}
-	c = regexp_match (reg_exp_t, current_panel->dir.list [i].fname, match_file);
-	if (c == -1){
+	c = regexp_match (reg_exp_t, current_panel->dir.list[i].fname,
+			  match_file);
+	if (c == -1) {
 	    message (1, MSG_ERROR, _("  Malformed regular expression  "));
 	    g_free (reg_exp);
 	    return;
 	}
-	if (c){
-	    do_file_mark (current_panel, i, 1);
+	if (c) {
+	    do_file_mark (current_panel, i, cmd);
 	}
     }
     g_free (reg_exp);
 }
 
-void unselect_cmd (void)
+void select_cmd (void)
 {
-    char *reg_exp, *reg_exp_t;
-    int i;
-    int c;
-    int dirflag = 0;
+    select_unselect_cmd (_(" Select "), 1);
+}
 
-    reg_exp = input_dialog (_(" Unselect "),"", easy_patterns ? "*" : ".");
-    if (!reg_exp)
-	return;
-    
-    reg_exp_t = reg_exp;
-    
-    /* Check if they specified directory matching */
-    if (*reg_exp_t == PATH_SEP){
-        dirflag = 1;
-	reg_exp_t ++;
-    }
-    if (reg_exp_t [strlen(reg_exp_t) - 1] == PATH_SEP){
-        dirflag = 1;
-        reg_exp_t [strlen(reg_exp_t) - 1] = 0;
-    }
-    for (i = 0; i < current_panel->count; i++){
-        if (!strcmp (current_panel->dir.list [i].fname, "..")) 
-            continue;
-	if (S_ISDIR (current_panel->dir.list [i].st.st_mode)){
-	    if (!dirflag)
-	        continue;
-        } else {
-            if (dirflag)
-                continue;
-        }
-	c = regexp_match (reg_exp_t, current_panel->dir.list [i].fname, match_file);
-	if (c == -1){
-	    message (1, MSG_ERROR, _("  Malformed regular expression  "));
-	    g_free (reg_exp);
-	    return;
-	}
-	if (c){
-	    do_file_mark (current_panel, i, 0);
-	}
-    }
-    g_free (reg_exp);
+void unselect_cmd (void)
+{
+    select)unselect_cmd (_(" Unselect "), 0);
 }
 
 /* Check if the file exists */


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