From 7cced6e57404cc1043a1b0dd0491aef2f792497e Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Thu, 12 Apr 2007 17:08:53 +0000 Subject: fix realloc-of-non-malloced pointer, and reduce size while at it --- shell/hush.c | 2 +- shell/lash.c | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/shell/hush.c b/shell/hush.c index 9af7f51..eae0e10 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -423,7 +423,7 @@ static const struct built_in_command bltins[] = { static const char *set_cwd(void) { if (cwd == bb_msg_unknown) - cwd = NULL; /* xrealloc_getcwd_or_warn(arg) called free(arg) */ + cwd = NULL; /* xrealloc_getcwd_or_warn(arg) calls free(arg)! */ cwd = xrealloc_getcwd_or_warn((char *)cwd); if (!cwd) cwd = bb_msg_unknown; diff --git a/shell/lash.c b/shell/lash.c index 5f2dacd..729d102 100644 --- a/shell/lash.c +++ b/shell/lash.c @@ -153,7 +153,7 @@ static int shell_context; /* Type prompt trigger (PS1 or PS2) */ /* Globals that are static to this file */ -static const char *cwd; +static char *cwd; static char *local_pending_command; static struct jobset job_list = { NULL, NULL }; static int argc; @@ -207,6 +207,14 @@ So cmd->text becomes child->family->text job_list becomes child->family->job_list */ + +static void update_cwd(void) +{ + cwd = xrealloc_getcwd_or_warn(cwd); + if (!cwd) + cwd = xstrdup(bb_msg_unknown); +} + /* built-in 'cd ' handler */ static int builtin_cd(struct child_prog *child) { @@ -220,9 +228,7 @@ static int builtin_cd(struct child_prog *child) bb_perror_msg("cd: %s", newdir); return EXIT_FAILURE; } - cwd = xrealloc_getcwd_or_warn((char *)cwd); - if (!cwd) - cwd = bb_msg_unknown; + update_cwd(); return EXIT_SUCCESS; } @@ -347,9 +353,7 @@ static int builtin_jobs(struct child_prog *child) /* built-in 'pwd' handler */ static int builtin_pwd(struct child_prog ATTRIBUTE_UNUSED *dummy) { - cwd = xrealloc_getcwd_or_warn((char *)cwd); - if (!cwd) - cwd = bb_msg_unknown; + update_cwd(); puts(cwd); return EXIT_SUCCESS; } @@ -1452,9 +1456,7 @@ static int busy_loop(FILE * input) #if ENABLE_FEATURE_CLEAN_UP static void free_memory(void) { - if (cwd && cwd != bb_msg_unknown) { - free((char*)cwd); - } + free(cwd); if (job_list.fg && !job_list.fg->running_progs) { remove_job(&job_list, job_list.fg); @@ -1571,14 +1573,12 @@ int lash_main(int argc_l, char **argv_l) } /* initialize the cwd -- this is never freed...*/ - cwd = xrealloc_getcwd_or_warn(NULL); - if (!cwd) - cwd = bb_msg_unknown; + update_cwd(); if (ENABLE_FEATURE_CLEAN_UP) atexit(free_memory); if (ENABLE_FEATURE_EDITING) cmdedit_set_initial_prompt(); else PS1 = NULL; - return (busy_loop(input)); + return busy_loop(input); } -- cgit v1.1