Fix a segfault in lash, hush, and cmdedit. Each of these used

xgetcwd, but did not check the return for a NULL, and then continued
to call strlen on the NULL when the cwd had been removed from under it.
 -Erik
This commit is contained in:
Eric Andersen 2001-05-11 16:58:46 +00:00
parent 9d94deabd3
commit 5f265b755a
10 changed files with 40 additions and 5 deletions

View File

@ -259,7 +259,7 @@ endif
LIBBB_MSRC=libbb/messages.c LIBBB_MSRC=libbb/messages.c
LIBBB_MESSAGES= full_version name_too_long omitting_directory not_a_directory \ LIBBB_MESSAGES= full_version name_too_long omitting_directory not_a_directory \
memory_exhausted invalid_date invalid_option io_error dash_dash_help \ memory_exhausted invalid_date invalid_option io_error dash_dash_help \
write_error too_few_args name_longer_than_foo write_error too_few_args name_longer_than_foo unknown
LIBBB_MOBJ=$(patsubst %,$(LIBBB)/%.o, $(LIBBB_MESSAGES)) LIBBB_MOBJ=$(patsubst %,$(LIBBB)/%.o, $(LIBBB_MESSAGES))

View File

@ -355,6 +355,10 @@ static void parse_prompt(const char *prmt_ptr)
char c; char c;
char *pbuf; char *pbuf;
if (!pwd_buf) {
pwd_buf=unknown;
}
while (*prmt_ptr) { while (*prmt_ptr) {
pbuf = buf; pbuf = buf;
pbuf[1] = 0; pbuf[1] = 0;

6
hush.c
View File

@ -429,6 +429,8 @@ static int builtin_cd(struct child_prog *child)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
cwd = xgetcwd(cwd); cwd = xgetcwd(cwd);
if (!cwd)
cwd = unknown;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -568,6 +570,8 @@ static int builtin_jobs(struct child_prog *child)
static int builtin_pwd(struct child_prog *dummy) static int builtin_pwd(struct child_prog *dummy)
{ {
cwd = xgetcwd(cwd); cwd = xgetcwd(cwd);
if (!cwd)
cwd = unknown;
puts(cwd); puts(cwd);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -2307,6 +2311,8 @@ int shell_main(int argc, char **argv)
/* initialize the cwd -- this is never freed...*/ /* initialize the cwd -- this is never freed...*/
cwd = xgetcwd(0); cwd = xgetcwd(0);
if (!cwd)
cwd = unknown;
#ifdef BB_FEATURE_COMMAND_EDITING #ifdef BB_FEATURE_COMMAND_EDITING
cmdedit_set_initial_prompt(); cmdedit_set_initial_prompt();
#else #else

View File

@ -282,5 +282,6 @@ extern const char * const dash_dash_help;
extern const char * const write_error; extern const char * const write_error;
extern const char * const too_few_args; extern const char * const too_few_args;
extern const char * const name_longer_than_foo; extern const char * const name_longer_than_foo;
extern const char * const unknown;
#endif /* __LIBBB_H__ */ #endif /* __LIBBB_H__ */

9
lash.c
View File

@ -297,7 +297,8 @@ static int builtin_cd(struct child_prog *child)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
cwd = xgetcwd(cwd); cwd = xgetcwd(cwd);
if (!cwd)
cwd = unknown;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -412,6 +413,9 @@ static int builtin_jobs(struct child_prog *child)
/* built-in 'pwd' handler */ /* built-in 'pwd' handler */
static int builtin_pwd(struct child_prog *dummy) static int builtin_pwd(struct child_prog *dummy)
{ {
cwd = xgetcwd(cwd);
if (!cwd)
cwd = unknown;
printf( "%s\n", cwd); printf( "%s\n", cwd);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -1827,7 +1831,6 @@ void free_memory(void)
{ {
if (cwd) { if (cwd) {
free(cwd); free(cwd);
cwd = NULL;
} }
if (local_pending_command) if (local_pending_command)
free(local_pending_command); free(local_pending_command);
@ -1919,6 +1922,8 @@ int shell_main(int argc_l, char **argv_l)
/* initialize the cwd -- this is never freed...*/ /* initialize the cwd -- this is never freed...*/
cwd = xgetcwd(0); cwd = xgetcwd(0);
if (!cwd)
cwd = unknown;
#ifdef BB_FEATURE_CLEAN_UP #ifdef BB_FEATURE_CLEAN_UP
atexit(free_memory); atexit(free_memory);

View File

@ -282,5 +282,6 @@ extern const char * const dash_dash_help;
extern const char * const write_error; extern const char * const write_error;
extern const char * const too_few_args; extern const char * const too_few_args;
extern const char * const name_longer_than_foo; extern const char * const name_longer_than_foo;
extern const char * const unknown;
#endif /* __LIBBB_H__ */ #endif /* __LIBBB_H__ */

View File

@ -58,4 +58,7 @@
#ifdef L_name_longer_than_foo #ifdef L_name_longer_than_foo
const char * const name_longer_than_foo = "Names longer than %d chars not supported."; const char * const name_longer_than_foo = "Names longer than %d chars not supported.";
#endif #endif
#ifdef L_unknown
const char * const unknown = "(unknown)";
#endif

View File

@ -355,6 +355,10 @@ static void parse_prompt(const char *prmt_ptr)
char c; char c;
char *pbuf; char *pbuf;
if (!pwd_buf) {
pwd_buf=unknown;
}
while (*prmt_ptr) { while (*prmt_ptr) {
pbuf = buf; pbuf = buf;
pbuf[1] = 0; pbuf[1] = 0;

View File

@ -429,6 +429,8 @@ static int builtin_cd(struct child_prog *child)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
cwd = xgetcwd(cwd); cwd = xgetcwd(cwd);
if (!cwd)
cwd = unknown;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -568,6 +570,8 @@ static int builtin_jobs(struct child_prog *child)
static int builtin_pwd(struct child_prog *dummy) static int builtin_pwd(struct child_prog *dummy)
{ {
cwd = xgetcwd(cwd); cwd = xgetcwd(cwd);
if (!cwd)
cwd = unknown;
puts(cwd); puts(cwd);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -2307,6 +2311,8 @@ int shell_main(int argc, char **argv)
/* initialize the cwd -- this is never freed...*/ /* initialize the cwd -- this is never freed...*/
cwd = xgetcwd(0); cwd = xgetcwd(0);
if (!cwd)
cwd = unknown;
#ifdef BB_FEATURE_COMMAND_EDITING #ifdef BB_FEATURE_COMMAND_EDITING
cmdedit_set_initial_prompt(); cmdedit_set_initial_prompt();
#else #else

View File

@ -297,7 +297,8 @@ static int builtin_cd(struct child_prog *child)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
cwd = xgetcwd(cwd); cwd = xgetcwd(cwd);
if (!cwd)
cwd = unknown;
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -412,6 +413,9 @@ static int builtin_jobs(struct child_prog *child)
/* built-in 'pwd' handler */ /* built-in 'pwd' handler */
static int builtin_pwd(struct child_prog *dummy) static int builtin_pwd(struct child_prog *dummy)
{ {
cwd = xgetcwd(cwd);
if (!cwd)
cwd = unknown;
printf( "%s\n", cwd); printf( "%s\n", cwd);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
@ -1827,7 +1831,6 @@ void free_memory(void)
{ {
if (cwd) { if (cwd) {
free(cwd); free(cwd);
cwd = NULL;
} }
if (local_pending_command) if (local_pending_command)
free(local_pending_command); free(local_pending_command);
@ -1919,6 +1922,8 @@ int shell_main(int argc_l, char **argv_l)
/* initialize the cwd -- this is never freed...*/ /* initialize the cwd -- this is never freed...*/
cwd = xgetcwd(0); cwd = xgetcwd(0);
if (!cwd)
cwd = unknown;
#ifdef BB_FEATURE_CLEAN_UP #ifdef BB_FEATURE_CLEAN_UP
atexit(free_memory); atexit(free_memory);