From 2253379eb864e3bd8668273d8ca5d9ff437702ba Mon Sep 17 00:00:00 2001 From: "Andrew V. Samoilov" Date: Fri, 3 Sep 2004 22:00:27 +0000 Subject: [PATCH] * subshell.c (exit_subshell): Prevent unwanted reuse of freed pty_buffer. * complete.c (variable_completion_function): strncpy()->memcpy() changes are because we know there is no '\0' among the first length bytes. (insert_text): Likewise. (command_completion_function): Avoid memory leaks. (filename_completion_function): g_free handles NULL argument too, no need for the comparison. (try_complete): Likewise. (command_completion_function): Likewise. (free_completions): Likewise. * widget.c (gauge_callback): Fix for 64-bit arches (%*s len must be int, %3d takes int. (copy_region): g_free handles NULL argument too, no need for the comparison. (kill_line): Likewise. * find.c (do_search): Likewise. (init_find_vars): Likewise. (do_find): Likewise. --- src/ChangeLog | 25 +++++++++++++++++++++++++ src/complete.c | 41 +++++++++++++++-------------------------- src/find.c | 37 +++++++++++++------------------------ src/subshell.c | 1 + src/widget.c | 8 +++----- 5 files changed, 57 insertions(+), 55 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 40a5f86af..5ad478c54 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,28 @@ +2004-09-04 Pavel S. Shirshov + + * subshell.c (exit_subshell): Prevent unwanted reuse of freed + pty_buffer. + * complete.c (variable_completion_function): strncpy()->memcpy() + changes are because we know there is no '\0' among the first + length bytes. + (insert_text): Likewise. + (command_completion_function): Avoid memory leaks. + (filename_completion_function): g_free handles NULL argument too, + no need for the comparison. + (try_complete): Likewise. + (command_completion_function): Likewise. + (free_completions): Likewise. + * widget.c (gauge_callback): Fix for 64-bit arches (%*s len must + be int, %3d takes int. + (copy_region): g_free handles NULL argument too, no need for + the comparison. + (kill_line): Likewise. + * find.c (do_search): Likewise. + (init_find_vars): Likewise. + (do_find): Likewise. + + Based on patch from Jakub Jelinek + 2004-09-03 Pavel S. Shirshov * menu.c (destroy_menu): Fix warnings. diff --git a/src/complete.c b/src/complete.c index dc1062715..2dd615bc8 100644 --- a/src/complete.c +++ b/src/complete.c @@ -66,12 +66,9 @@ filename_completion_function (char *text, int state) if (!state){ char *temp; - if (dirname) - g_free (dirname); - if (filename) - g_free (filename); - if (users_dirname) - g_free (users_dirname); + g_free (dirname); + g_free (filename); + g_free (users_dirname); if ((*text) && (temp = strrchr (text, PATH_SEP))){ filename = g_strdup (++temp); @@ -156,18 +153,12 @@ filename_completion_function (char *text, int state) mc_closedir (directory); directory = NULL; } - if (dirname){ - g_free (dirname); - dirname = NULL; - } - if (filename){ - g_free (filename); - filename = NULL; - } - if (users_dirname){ - g_free (users_dirname); - users_dirname = NULL; - } + g_free (dirname); + dirname = NULL; + g_free (filename); + filename = NULL; + g_free (users_dirname); + users_dirname = NULL; return NULL; } else { char *temp; @@ -258,7 +249,7 @@ variable_completion_function (char *text, int state) *temp = '$'; if (isbrace) temp [1] = '{'; - strncpy (temp + 1 + isbrace, *env_p, p - *env_p); + memcpy (temp + 1 + isbrace, *env_p, p - *env_p); if (isbrace) strcpy (temp + 2 + (p - *env_p), "}"); else @@ -433,8 +424,7 @@ command_completion_function (char *text, int state) words = bash_reserved; phase = 0; text_len = strlen (text); - path = getenv ("PATH"); - if (path) { + if (!path && (path = getenv ("PATH")) != NULL) { p = path = g_strdup (path); path_end = strchr (p, 0); while ((p = strchr (p, PATH_ENV_SEP))) { @@ -498,8 +488,8 @@ command_completion_function (char *text, int state) if (!found) { look_for_executables = 0; - if (path) - g_free (path); + g_free (path); + path = NULL; return NULL; } if ((p = strrchr (found, PATH_SEP)) != NULL) { @@ -756,8 +746,7 @@ try_complete (char *text, int *start, int *end, int flags) } } - if (word) - g_free (word); + g_free (word); return matches; } @@ -801,7 +790,7 @@ static int insert_text (WInput *in, char *text, ssize_t len) *(p++) = *(q++); *p = 0; } - strncpy (in->buffer + start, text, len - start + end); + memcpy (in->buffer + start, text, len - start + end); in->point += len; update_input (in, 1); end += len; diff --git a/src/find.c b/src/find.c index a581924cb..96974e056 100644 --- a/src/find.c +++ b/src/find.c @@ -512,10 +512,8 @@ do_search (struct Dlg_head *h) mc_closedir (dirp); dirp = 0; } - if (directory) { - g_free (directory); - directory = NULL; - } + g_free (directory); + directory = NULL; dp = 0; return 1; } @@ -553,10 +551,7 @@ do_search (struct Dlg_head *h) break; } - if (directory) { - g_free (directory); - directory = NULL; - } + g_free (directory); directory = tmp; if (verbose){ @@ -630,10 +625,8 @@ init_find_vars (void) { char *dir; - if (old_dir){ - g_free (old_dir); - old_dir = 0; - } + g_free (old_dir); + old_dir = 0; count = 0; matches = 0; @@ -948,10 +941,9 @@ find_file (char *start_dir, char *pattern, char *content, char **dirname, kill_gui (); do_search (0); /* force do_search to release resources */ - if (old_dir) { - g_free (old_dir); - old_dir = 0; - } + g_free (old_dir); + old_dir = 0; + return return_value; } @@ -984,17 +976,14 @@ do_find (void) do_cd (filename, cd_exact); select_item (current_panel); } - if (dirname) - g_free (dirname); - if (filename) - g_free (filename); + g_free (dirname); + g_free (filename); break; } - if (content) - g_free (content); + g_free (content); dir_and_file_set = dirname && filename; - if (dirname) g_free (dirname); - if (filename) g_free (filename); + g_free (dirname); + g_free (filename); if (v == B_CANCEL) break; diff --git a/src/subshell.c b/src/subshell.c index aa5cdb243..7e8a093fe 100644 --- a/src/subshell.c +++ b/src/subshell.c @@ -659,6 +659,7 @@ exit_subshell (void) g_free (subshell_prompt); g_free (pty_buffer); subshell_prompt = NULL; + pty_buffer = NULL; return quit; } diff --git a/src/widget.c b/src/widget.c index 036f178e7..ced4a0d2c 100644 --- a/src/widget.c +++ b/src/widget.c @@ -620,7 +620,7 @@ gauge_callback (WGauge *g, int msg, int parm) if (!g->shown) printw ("%*s", gauge_len, ""); else { - long percentage, columns; + int percentage, columns; long total = g->max, done = g->current; if (total <= 0 || done < 0) { @@ -1241,8 +1241,7 @@ copy_region (WInput *in, int x_first, int x_last) if (last == first) return; - if (kill_buffer) - g_free (kill_buffer); + g_free (kill_buffer); kill_buffer = g_strndup(in->buffer+first,last-first); } @@ -1323,8 +1322,7 @@ yank (WInput *in) static void kill_line (WInput *in) { - if (kill_buffer) - g_free (kill_buffer); + g_free (kill_buffer); kill_buffer = g_strdup (&in->buffer [in->point]); in->buffer [in->point] = 0; }